是否可以访问 Gridview 中模板字段中的对象(按钮或超链接)

本文关键字:对象 按钮 超链接 访问 Gridview 是否 字段 | 更新日期: 2023-09-27 18:31:52

我想访问网格视图中模板字段中的对象(超链接或按钮)。我该怎么做?

是否可以访问 Gridview 中模板字段中的对象(按钮或超链接)

假设您要将数据绑定到它,您需要查看在 RowDataBound 事件中执行此操作。

下面是如何在模板字段中检索控件的示例:

。.aspx:

<asp:GridView ID="GridView1" Runat="server" 
    OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Template Field">
            <ItemTemplate>
                <asp:Button ID="btnTest" Runat="server" /> 
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button btnTest = (Button)e.Row.FindControl("btnTest");
        btnTest.Text = "I'm in a Template Field";
    }
}

您可以在模板控件的RowDataBound或单击事件上使用它,例如

TextBox txtTemp= (TextBox )e.Row[e.RowIndex].FindControl("yourControlName");
string someText=txtTemp.Text;