views:

1617

answers:

1

Hi folks,

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

+1  A: 

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.

Mark Brackett
I instead added them through the hierarchy in OnDataBound, but unfortunately the row disappears if a postback occurs without binding the data. Additionally, though they are now clickable and the ID seems correct, there doesn't seem to be an event thrown.
Nick Spacek
You have to re-add them (since they're dynamic controls) on postback to get the event to fire.
Mark Brackett