页面加载时如何将asp.net网格视图代码与网格视图一起加载?

时间:2019-03-15 14:35:12

标签: c# asp.net

我有带有Boundfield列的网格视图,并试图在页面加载时替换后面代码中的Boundfield标头文本。根据业务,HeaderText必须更改,但就我而言,文本不会立即更改。

<asp:GridView ID="sampleGrid" runat="server" OnRowDataBound="sampleGrid_RowDataBound" OnSorting="sampleGrid_Sorting">
    <Columns>
        <asp:BoundField HtmlEncode="False" HeaderText="Name" DataField="Name"  SortExpression="Name" />
    </Columns>
</asp:GridView>

后面的代码:

protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row != null)
    {
        sampleGrid.Columns[0].HeaderText = "text";
    }
}

我添加了代码的主要逻辑。请让我知道我的问题是否有解决方案。

提前感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

您可以在RowDataBound事件中的单元格级别上设置“标题行”的值。

protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "text";
    }
}

更新

if (e.Row.RowType == DataControlRowType.Header)
{
    LinkButton lb = e.Row.Cells[0].Controls[0] as LinkButton;
    lb.Text = "text";
}

答案 1 :(得分:1)

您可以尝试以下

protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        LinkButton headerText = e.Row.Cells[0].Controls[0] as LinkButton; 
        headerText.Text = "Michel";
    }
}