views:

32

answers:

2

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.

A: 

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
}
Zain Shaikh
Still I am getting null
Nadeem
just to make sure everything is fine, check if ViewState of page is enabled.
Zain Shaikh
and one more thing, you must be assigning some data to GridView in the page load event, make sure you are assigning datasource inside if(!Page.IsPostback) { } condition.
Zain Shaikh
Ohh !!! I tried after disabling the ViewState and now it is working fine.
Nadeem
:s how would you get the state of a control after disabling the ViewState.?
Zain Shaikh
I am also surprised. I tried EnableViewState="false" in the page directive and now I can get the value of the Textbox
Nadeem
and the other controls on page are working fine after postbacks?
Zain Shaikh
Everything seems to be working fine. The only thing which I can't understand is what goes wrong when I have ViewState as Enabled.
Nadeem
that is just weird. no idea. sorry.
Zain Shaikh
A: 

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.

Mazhar Karimi