Nick Spacek
Nick Spacek

Reputation: 4773

Custom Header in GridView

I've already got my custom header drawing in my GridView using SetRenderMethodDelegate on the header row within the OnRowCreated method. I'm having problems trying to add LinkButtons to the new header row though.

This is what the RenderMethod looks like:

private void RenderSelectionMode(HtmlTextWriter output, Control container)
{
    TableHeaderCell cell = new TableHeaderCell();
    cell.Attributes["colspan"] = container.Controls.Count.ToString();
    AddSelectionModeContents(cell);
    cell.RenderControl(output);

    output.WriteEndTag("tr");

    HeaderStyle.AddAttributesToRender(output);
    output.WriteBeginTag("tr");

    for(int i = 0; i < container.Controls.Count; i++)
    {
        DataControlFieldHeaderCell cell = (DataControlFieldHeaderCell)container.Controls[i];
        cell.RenderControl(output);
    }
}

private void AddSelectionModeContents(Control parent)
{
    // TODO: should add css classes

    HtmlGenericControl label = new HtmlGenericControl("label");
    label.InnerText = "Select:";

    selectNoneLK = new LinkButton();
    selectNoneLK.ID = "SelectNoneLK";
    selectNoneLK.Text = "None";
    //selectNoneLK.Attributes["href"] = Page.ClientScript.GetPostBackClientHyperlink(selectNoneLK, "");
    //selectNoneLK.Click += SelectNoneLK_Click;
    selectNoneLK.Command += SelectNoneLK_Click;

    selectAllLK = new LinkButton();
    selectAllLK.ID = "SelectAllLK";
    selectAllLK.Text = "All";
    //selectAllLK.Attributes["href"] = Page.ClientScript.GetPostBackClientHyperlink(selectAllLK, "");
    //selectAllLK.Click += SelectAllLK_Click;
    selectAllLK.Command += SelectAllLK_Click;

    parent.Controls.Add(label);
    parent.Controls.Add(selectNoneLK);
    parent.Controls.Add(selectAllLK);
}

As you can see, I have tried different ways to get my LinkButtons working (none have worked though). The LinkButtons are rendered as plain anchor tags, like this: <a id="SelectNoneLK">None</a>

I know there is something wrong with the fact that the ID looks like that, since I am using a Master page for this and the ID should be something much longer.

Any help would be appreciated!

Nick

Upvotes: 0

Views: 4245

Answers (1)

Mark Brackett
Mark Brackett

Reputation: 85645

I'd guess that since cell is not part of the control hierarchy (you never add it to the table), the LinkButton's never find an IContainer parent to rewrite their ID's.

I tend to solve these types of issues using the excellent RenderPipe control that allows me to declare my controls in one place, but render them somewhere else.

Upvotes: 1

Related Questions