Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 40 rows and 16 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.NET
<asp:GridView ID="gv1" runat="server" Visible="true" ShowHeader="true" AutoGenerateColumns="false" >
    <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"
                 >
            </itemtemplate>
        
 </columns>

Code Behind for Gridview Data binding:
C#
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:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in gv1.Rows)
    {
        TextBox textBoxq = row.FindControl("txtcol2") as TextBox;
        // OR  below code is also not working
        //for this i do Gridview and textboxes enableviewstate false but it also not working.
        
        //value of boundfield is access but value of textboxes is not access...but in textshow null      
    }
}

Please give me a solution??????????????????


[Edit member="Tadit"]
Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]
Posted
v3
Comments
TextBox textBoxq = row.FindControl("txtcol2") as TextBox;

What is happening here? Is it null? What is the value of textBoxq.Text after this line?
Sinisa Hajnal 3-Oct-14 3:49am    
Check the exact HTML generated within the grid. Maybe your textbox is wrapped into some element.
[no name] 3-Oct-14 4:11am    
wt the exact issue did u face?
check you html code

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