将 HeaderText 绑定到后面代码中的字符串

Binding HeaderText to string in code behind

我正在调试一些包含 .aspx 文件和隐藏代码的 c# 文件的旧代码。我在弄清楚如何为 TemplateField 绑定 'HeaderText' 中的文本时遇到了一些问题。 (注意:以前从未使用过 aspx)。基本上我在代码隐藏中有一个字符串数组,在 .aspx 文件中有几个不同的 TemplateFields。我想将这些字段的 HeaderText 绑定到字符串。我知道对于 TextBoxes 它将是

Text = '<%# bindingStuffHere %> 

举个例子,假设我有一个如下所示的模板字段:

<asp:TemplateField HeaderText=""  >

在后面的代码中我有:

String[] days = new String[5]
days[0] = "SAT"

我想将 "SAT" 绑定到 HeaderText。模板位于 GridView 中。

您无法在 asp:TemplateField 中绑定 HeaderText。相反,您可以实现 OnRowDataBound 事件并更改那里的 header 文本。

protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Header)
     {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Text = days[i];
        }
     }
}