从动态Gridview文本框访问值

从动态Gridview文本框访问值

本文介绍了从动态Gridview文本框访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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;

            }
    }

推荐答案


这篇关于从动态Gridview文本框访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:44