Mohammad Nadeem
Mohammad Nadeem

Reputation: 9392

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='<%# 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.

Upvotes: 2

Views: 6933

Answers (4)

LiFe
LiFe

Reputation: 21

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

Upvotes: 2

Tanaji Kamble
Tanaji Kamble

Reputation: 363

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

Upvotes: 0

Mazhar Karimi
Mazhar Karimi

Reputation:

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.

Upvotes: -1

Zain Shaikh
Zain Shaikh

Reputation: 6043

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
}

Upvotes: 0

Related Questions