从gridview中的emptydatatemplate中查找控件

时间:2013-02-13 06:28:50

标签: asp.net gridview

我使用EmptyDataTemplate在网格中输入新数据,而没有现有数据,但我无法在EmptyDateTemplate中找到我的控件

protected void gvNavigationDtls_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("EInsert"))
        {
            GridViewRow emptyrow = gvNavigationDtls.Controls[0].Controls[0] as GridViewRow;
            if (((TextBox)emptyrow.FindControl("txtCode")).Text == "")

在页面加载中我也通过编写以下代码来检查

gvNavigationDtls.DataBind();
            Control c = gvNavigationDtls.Controls[0].FindControl("txtCode");
            if (c != null)
            {
            }

但是c为null,这意味着我无法找到使用它的控件, 请帮忙,先谢谢

2 个答案:

答案 0 :(得分:3)

protected void gvNavigationDtls_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("EInsert"))
        {
          TextBox txtCode=(TextBox)gvNavigationDtls.Controls[0].Controls[0].FindControl("txtCode");
          txtCode.Text="Your value";
        }
     }

答案 1 :(得分:0)

实际上,只是遇到了这个问题,并且很容易找到访问数据但它在RowDataBound事件中。

    Protected Sub grdView(sender As Object, e As GridViewRowEventArgs) Handles grdView.RowDataBound
        If e.Row.RowType = DataControlRowType.EmptyDataRow Then
            Dim txt As TextBox = e.Row.FindControl("txtBox")
            txt.Text = "Hey, this works!"
        End If
    End Sub