1
Reply

Get values of Gridview TextBox on Button Click

Pankaj Singh

Pankaj Singh

Apr 18 2012 9:28 PM
15.3k
Hi,

I have a ITEMTemplate TextBox in a Gridview. I want to save values of Texboxes on button click. I am using following code ::

Source code:

<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="Values">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Insert Into Table" />
</form>

cs file:

protected void Page_Load(object sender, EventArgs e)
{
GetDetails();
}
private void GetDetails()
{
da = new SqlDataAdapter("select column_name from testing.information_schema.columns where table_name = 'emp'", con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
TextBox box1 = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1");
string s = box1.Text;
}
}

But it is not working . I am getting s="" . please help me.

Thanks in advance.

Pankaj Singh





Answers (1)