How to access a textbox text from gridview to a button Onclick event

11,211

You can do like this:

protected void btnSave1_Click(object sender, EventArgs e)
{
     GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
     TextBox TextBox1 = row.FindControl("TextBox1") as TextBox; 

      //Access TextBox1 here.
      string myString = TextBox1.Text;
}
Share:
11,211
Aayushi Jain
Author by

Aayushi Jain

Updated on June 15, 2022

Comments

  • Aayushi Jain
    Aayushi Jain almost 2 years

    I am having textboxes and buttons in gridview. What I want is that when I click on those buttons the corresponding value in textbox is updated to database, so I need to access the textbox text in OnClick event. How can I do this?

    Gridview:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                BackColor="#CCCCCC" BorderColor="#999999" 
                BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" 
                ForeColor="Black" OnRowDataBound="GridView1_RowDataBound" 
                onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField HeaderText="Save 1">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" Width="30px" runat="server"></asp:TextBox>
                            <asp:Button ID="btnSave1" runat="server" Text="Save"  OnClick="btnSave1_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Save 2">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" Width="30px" runat="server"></asp:TextBox>
                            <asp:Button ID="btnSave2" runat="server" Text="Save"  OnClick="btnSave2_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
    </asp:GridView>
    

    Button Onclick Event:

    protected void btnSave1_Click(object sender, EventArgs e)
        {
             //I want to access TextBox1 here.
        }
    

    Please help me out of this.

  • Aayushi Jain
    Aayushi Jain over 10 years
    But since my button click event is outside gridview, e.RowIndex will not work.
  • Ricky Stam
    Ricky Stam over 10 years
    on click event trigger Update Command. The website i provided you will give you the details. it's very close to what you want
  • Aayushi Jain
    Aayushi Jain over 10 years
    In the site example, they are using FooterTemplate, can you tell me where I should use it?
  • Aayushi Jain
    Aayushi Jain over 10 years
    Although it is working but I have to create footer for this which I dont want. Do you have any other method?
  • Ricky Stam
    Ricky Stam over 10 years
    Check the DeleteCustomer Function and also the Gridview Template. He passes a Command Argument with the value he wants to delete? From your example i don't see any datasource. If you don't have need to present data from database but only save new records you can use simple HTML5 table
  • Ricky Stam
    Ricky Stam over 10 years
    Either Footer Template or Item Template the code is same there is no difference
  • Aayushi Jain
    Aayushi Jain over 10 years
    Although it is working but the textbox value is coming null. Why?