在gridview中查找控件返回null

本文关键字:返回 null 控件 查找 gridview | 更新日期: 2023-09-27 18:16:38

我有一个用asp.net (c#)编写的gridview,问题是当尝试从文本框读取数据返回null时。

       <asp:GridView ID="GridView1" runat="server"  DataKeyNames="Week#/Day" 
            OnRowDataBound="GridView1_RowDataBound"
             onrowediting="GridView1_RowEditing" 
              OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="false"
              OnPageIndexChanging="GridView1_PageIndexChanging"
            OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="true"
         PageSize="4" >
            <Columns>
             <asp:ButtonField ButtonType="Link" CommandName="Update" text="Update" />
             <asp:ButtonField ButtonType="Link" CommandName="Edit"  text="Edit"/
       <asp:TemplateField HeaderText="Week#/Day" InsertVisible="False"
      SortExpression="Week#/Day">
                  <EditItemTemplate>
                      <asp:Label ID="Label5" runat="server" Text='<%#
      Eval("Week#/Day") %>'></asp:Label>
                  </EditItemTemplate>
              </asp:TemplateField>
         <asp:TemplateField HeaderText="Saturday" SortExpression="Saturday">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox1" runat="server"
          Text='<%# Bind("Saturday") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label7" runat="server"
           Text='<%# Bind("Saturday") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
            <asp:TemplateField HeaderText="Sunday" SortExpression="Sunday">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox2" runat="server"
              Text='<%# Bind("Sunday") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label8" runat="server"
               Text='<%# Bind("Sunday") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                   <asp:TemplateField HeaderText="Monday" SortExpression="Monday">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox3" runat="server"
               Text='<%# Bind("Monday") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label9" runat="server"
                 Text='<%# Bind("Monday") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                   <asp:TemplateField HeaderText="Tuesday" SortExpression="Tuesday">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox4" runat="server"
                Text='<%# Bind("Tuesday") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label10" runat="server"
              Text='<%# Bind("Tuesday") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                   <asp:TemplateField HeaderText="Wednesday"
                SortExpression="Wednesday">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox5" runat="server"
         Text='<%# Bind("Wednesday") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label11" runat="server"
         Text='<%# Bind("Wednesday") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
           </Columns>
    </asp:GridView>

我是用datatable在编辑上填充它调用这个方法

      protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }

和on update调用update方法

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    { 
            //Retrieve the table from the session object.
            DataTable dt = (DataTable)Session["All_Topics"];
           GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            TextBox d1, d2, d3, d4, d5;
            d1 = (TextBox)row.FindControl("TextBox1");
            d2 = (TextBox)row.FindControl("TextBox2");
            d3 = (TextBox)row.FindControl("TextBox3");
            d4 = (TextBox)row.FindControl("TextBox4");
            d5 = (TextBox)row.FindControl("TextBox5");    
            dt.Rows[row.DataItemIndex]["Saturday"] = d1.Text;
            dt.Rows[row.DataItemIndex]["Sunday"] = d2.Text;
            dt.Rows[row.DataItemIndex]["Monday"] = d3.Text;
            dt.Rows[row.DataItemIndex]["Tuesday"] = d4.Text;
            dt.Rows[row.DataItemIndex]["Wednesday"] = d5.Text;
       }

问题是当从文本框读取数据时,它总是为空。如何解决这个问题?

在gridview中查找控件返回null

嘿,请确保不要在页面的PostBack上重新绑定GridView。这可能是问题的原因。

始终在页面加载时将网格绑定在下面的代码中。

if (!Page.IsPostBack ){
// Code to bind the Grid
// BindData();
}

当你点击Row Update Command时,它首先进入页面加载事件,如果你没有像给定的方式绑定你的网格,你的网格将被重新绑定,你将从文本框中得到null。