Click here to Skip to main content
14,739,758 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
<pre lang="c#">

Dear friends,

I want to enter and insert all rows in one go from Gridview textboxes into database , initially all textboxes are empty. I have 33 rows and 7 columns of text box in Gridview and 1 column for display purpose in unicode (extreme left) .

For simplicity of code I have written only 2 column here, one for display purpose as first column and second column as text box for inserting data from user.

I have difficulty in accessing value from dynamic bound Gridview Textbox under ItemTemplate.

Please help me out in fetching values from Gridview textbox after button click.


HTML:
 <asp:GridView ID="gv1" runat="server" Visible="true" ShowHeader="true" AutoGenerateColumns="false" EnableViewState="true">
                                    <Columns>
                                        <asp:BoundField DataField="Column1" HeaderText="1" />
                                        <asp:TemplateField HeaderText="2">
                                            <ItemTemplate>
 <asp:TextBox ID="txtcol2" Text='<%# Eval("Column2") %>' EnableViewState="true" runat="server" MaxLength="7" Width="75px"
                                                 ></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                 </Columns>
  </asp:GridView>



Code Behind for Gridview Data binding:

 private void BindGrid()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dr = dt.NewRow();
        dr["Column1"] = "कर /पथकर शुल्कों से आय"; //In Hindi
        dr["Column2"] = string.Empty;
        dt.Rows.Add(dr);
        gv1.DataSource = dt;
        gv1.DataBind();
}


Code Behind for fetching value from Gridview after button click to  insert all rows into database:

 protected void Button1_Click(object sender, EventArgs e)
    {
foreach (GridViewRow row in gv1.Rows)
            {
                 TextBox box2 = (TextBox)row.Cells[1].Controls[1];
                string    str = box2.Text;
// OR  below code is also not working

             string txtSerAmt = ((TextBox)row.Cells[1].FindControl("txtcol2")).Text;
// OR  below code is also not working
               string txtSerAmt = ((TextBox)row.FindControl("txtcol2")).Text;
              
            }
    }
Posted

1 solution

Answer is:

Just has to EnableViewState="false" for both Gridview and textbox.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900