Custom Header in GridView

时间秒杀一切 提交于 2019-12-25 04:59:09

问题


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:


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.



来源:https://stackoverflow.com/questions/509029/custom-header-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!