在gridview中查找控件返回null

时间:2013-06-01 10:10:56

标签: c# asp.net gridview

我有一个用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>

我在编辑调用此方法时使用数据表填充它

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

        BindData();
    }

以及更新调用更新方法

    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;

       }

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

希望它会对你有所帮助。