如何获取gridview中texbox的动态值,这是在下拉列表中生成的,所选索引已更改

时间:2017-05-19 06:17:20

标签: c# asp.net

我在textbox内创建了动态gridview,这是dropdown 所选索引更改时生成的,但现在我想保存textbox值在数据库中,但如何?

  

在那里,一行包含五个动态创建的文本框。现在,   如何在每个提交按钮单击时获取这些textbox值   行?

任何人都可以帮助我这个领域的新人所以指导我......谢谢

<asp:GridView ID="GridViewclass" runat="server" 
OnRowDataBound="GridViewclass_RowDataBound"
OnDataBound="GridViewclass_DataBound"
AutoGenerateColumns="False" AutoGenerateSelectButton="true"
HeaderStyle-BackColor="#174A63" HeaderStyle-ForeColor="white" 
AllowPaging="true" Font-Size="14px"
Style="margin-left: 16%;" PageSize="15" Width="300px">
<SelectedRowStyle CssClass="selectedRowStyle" />

<Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
    <asp:TemplateField HeaderText="Header 1">
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Header 2">
        <ItemTemplate>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Header 3">
        <ItemTemplate>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Header 4">
        <ItemTemplate>
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Header 5">
        <ItemTemplate>
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

//Code Behind

protected void ddlClassUnits_SelectedIndexChanged(object sender,EventArgs e)    
{
     try
      {
    int createrows = 0;
    int GroupId = Convert.ToInt32(ddlClassUnits.SelectedValue);
    if (GroupId == 0)
    {
        ClearContentControls(4);
    }
    else
    {
        if (GroupId == 1)
        {
            createrows = 1;
        }
        if (GroupId == 2)
        {
            createrows = 2;
        }

        GridViewclass.Visible = true;
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));

        for (int i = 0; i < createrows; i++)
        {
            dr = dt.NewRow();
            dr["RowNumber"] = i + 1; ;
            dr["Column1"] = string.Empty;
            dr["Column2"] = string.Empty;
            dr["Column3"] = string.Empty;
            dr["Column4"] = string.Empty;
            dr["Column5"] = string.Empty;
            dt.Rows.Add(dr);
        }

        GridViewclass.DataSource = dt;
        GridViewclass.DataBind();
        ViewState["CurrentTable"] = dt;
    }
  }
  catch (Exception ex)
  {
  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用TextBox声明从GridView中读取((TextBox)row.FindControl("TextBox1")).Text值。 我创建了一个Button来获取GridViewDataTable的价值。现在,您可以Insert将这些值Database转换为protected void Button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Column1", typeof(string))); dt.Columns.Add(new DataColumn("Column2", typeof(string))); dt.Columns.Add(new DataColumn("Column3", typeof(string))); dt.Columns.Add(new DataColumn("Column4", typeof(string))); dt.Columns.Add(new DataColumn("Column5", typeof(string))); DataRow dr = null; foreach (GridViewRow row in GridViewclass.Rows) { dr = dt.NewRow(); dr["RowNumber"] = row.Cells[1].Text; dr["Column1"] = ((TextBox)row.FindControl("TextBox1")).Text; dr["Column2"] = ((TextBox)row.FindControl("TextBox2")).Text; dr["Column3"] = ((TextBox)row.FindControl("TextBox3")).Text; dr["Column4"] = ((TextBox)row.FindControl("TextBox4")).Text; dr["Column5"] = ((TextBox)row.FindControl("TextBox5")).Text; dt.Rows.Add(dr); } //this will insert data of dt into database BulkDataInsert(dt, "connectionString"); } void BulkDataInsert(DataTable dataTable, string connectionString) { try { SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.TableLock) { DestinationTableName = txtTableName.Text, BatchSize = 100000, BulkCopyTimeout = 360 }; bulkCopy.WriteToServer(dataTable); //Data Inserted Succesfull } catch (Exception ex) { //ex.Message) } } 。请查看以下示例。

代码:

BulkDataInsert

我已经创建了一个方法DataTable,这会将完整的Database插入DataTable。您需要传递Database的{​​{1}}和连接字符串

输出:enter image description here