2013-06-01 55 views
0

我有一个在asp.net(c#)中编写的gridview,问题是当试图从文本框中读取数据时返回null。在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; 

     } 

的问题是从文本中读取数据时,它总是填充空值。我怎么解决这个问题。

回答

0

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

总是绑定你的网格页面加载在下面的代码。

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

当你点击Row Update Command它首先进入的页面加载事件,并在页面加载,如果你不绑定您的网格状给定的方式网格是重新绑定,你会得到从文本框为空。

希望它能帮助你。

相关问题