Ahtesham
Ahtesham

Reputation: 301

I can't get the value of textbox from the footer row of gridview

i can't get the value of textbox from the footer row of gridview

    <asp:GridView ID="GridView1" runat="server" Width="1214px"       
            AutoGenerateColumns="False" ShowFooter="true" 
 OnRowCommand="GridView1_RowCommand"                                  
  <Columns>
 <asp:TemplateField HeaderText="Insert">
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Student Name">
            <EditItemTemplate>
               <asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtSName" runat="server"/>
            </FooterTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>

and the code behind is........ i can't get the value of textbox from the footer row of gridview

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName==("AddNew"))
    {
        TextBox txtName =(TextBox) GridView1.FooterRow.FindControl("txtSName");
string strName=txtName.Text;   //strName is Empty while i m enters data into the  textbox txtSName
     }

Upvotes: 0

Views: 3163

Answers (2)

sasha
sasha

Reputation: 21

Posting this comment to help others who may encounter the same issue.

Make sure the grid is not loading again on post back.

code in the page load should look like this

if (!IsPostBack)
{
  LoadGrid();
}

Upvotes: 2

abidmix
abidmix

Reputation: 1748

Your mark up is biting you.You are having two footer templates within a template field. Do this

<asp:TemplateField HeaderText="Insert">
     <ItemTemplate>
        <asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
            ImageUrl="~/images/Edit.png"Style="height: 16px" ToolTip="Edit" />
    </ItemTemplate>
    <EditItemTemplate>
       <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" 
            CausesValidation="false" OnClientClick="return confirm('Delete.Are you sure you want to delete?')" 
            ImageUrl="~/images/DeleteTS.png" Text="Cancel" />
    </EditItemTemplate>
    <FooterTemplate>
          <asp:ImageButton ID="AddNewImgBtn" runat="server" ImageUrl="~/images/saveHS.png"
               ToolTip="Add New" AlternateText="Add New" Width="16px" Height="16px" 
               CommandArgument="InsertNew" ImageAlign="AbsMiddle" />
    </FooterTemplate>             
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Student Name">
     <EditItemTemplate>
          <asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>' />
     </EditItemTemplate>
      <ItemTemplate>
            <asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
      </ItemTemplate>
      <FooterTemplate>
          <asp:TextBox ID="txtSName" runat="server"/>
      </FooterTemplate>
      </asp:TemplateField>
  </Columns>
  </asp:GridView>

And then you can gracefully locate your footer row.

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
           if (e.CommandArgument=="InsertNew")
         {
         GridView testGrid=(Gridview)sender;
           TextBox txtName =(TextBox)testGrid.FooterRow.FindControl("txtSName");
        string strName=txtName.Text;   
          }
         }

See working example Example

Upvotes: 1

Related Questions