开发者

Gridview FooterRow textbox text is null when accessing from code behind

I have a GridView with the following columns

     <asp:TemplateField HeaderText="Name">
         <FooterTemplate>
             <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
         </FooterTemplate>
       <ItemTemplate>
         <asp:Label ID="lbl_name" runat="server" Text='开发者_JAVA百科<%# DataBinder.Eval(Container.DataItem, "t_Name") %>' />             
       </ItemTemplate>  
       <EditItemTemplate>
         <asp:TextBox ID="txt_name" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"t_Name") %>'></asp:TextBox>
       </EditItemTemplate>        
     </asp:TemplateField>

     <asp:TemplateField HeaderText="Created By">
       <ItemTemplate>
          <asp:Label ID="lbl_tabcreatedby" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "t_CreatedBy") %>' />
       </ItemTemplate>
     </asp:TemplateField>

       <asp:CommandField HeaderText="Modify" ShowEditButton="True" />

       <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

       <asp:TemplateField HeaderText="Add a New Name">
           <FooterTemplate>
               <asp:LinkButton ID="lnkbtn_AddName" runat="server" CommandName="Insert">Add Name</asp:LinkButton>
           </FooterTemplate>
       </asp:TemplateField>

And then in the Code Behind I am trying to access the txt_Name Textbox as

protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
{             
  string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text;
  // Insert Code
}

But I am getting null in the string t_Name everytime irrespective of what is the current Text of txt_Name. However I can get the text if I disable the ViewState for the page. Any explanation.


I have got around this problem by using an additional variable, see the following:

Dim txtBox As TextBox = GridView1.FooterRow.FindControl("txtName")
Dim name As String = txtBox.Text


or you can try getting the textbox by column index, like following:

protected void gv_Name_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string t_Name = ((TextBox)(gv_Name.FooterRow.Cells[5].FindControl("txt_Name"))).Text;
    // Insert Code
}


Try following code in gv_Name_RowCommand event

if (e.CommandName.Equals("Insert"))
 {
string t_Name = ((TextBox)(gv_Name.FooterRow.FindControl("txt_Name"))).Text; 
} 

this should work


i think you data grid is being disconnected from the data source upon post back. If you are sure that data/ data value coming from db is not null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜