【问题标题】:Custom Header in GridViewGridView 中的自定义标题
【发布时间】:2009-02-03 21:42:50
【问题描述】:

我已经在我的 GridView 中使用 OnRowCreated 方法的标题行上的 SetRenderMethodDelegate 绘制了我的自定义标题。我在尝试将 LinkBut​​tons 添加到新标题行时遇到问题。

这是 RenderMethod 的样子:

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);
}

如您所见,我尝试了不同的方法来让我的 LinkBut​​tons 工作(虽然没有一个工作)。 LinkBut​​tons 呈现为普通锚标记,如下所示:None

我知道 ID 看起来是这样的事实有问题,因为我为此使用母版页,并且 ID 应该更长。

任何帮助将不胜感激!

尼克

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我猜由于单元格不是控件层次结构的一部分(您永远不会将其添加到表中),LinkBut​​ton 永远不会找到 IContainer 父级来重写其 ID。

    我倾向于使用出色的RenderPipe control 来解决这些类型的问题,它允许我在一个地方声明我的控件,但在其他地方呈现它们。

    【讨论】:

    • 我改为通过 OnDataBound 中的层次结构添加它们,但不幸的是,如果在没有绑定数据的情况下发生回发,则该行会消失。此外,尽管它们现在可以点击并且 ID 似乎正确,但似乎没有引发任何事件。
    • 您必须在回发时重新添加它们(因为它们是动态控件)才能触发事件。
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多