0

我在弹出窗口中有一个 gridview,有 3 列,其中 2 列是文本框列。我在行数据绑定事件中动态添加了文本框。当输入数据并单击保存按钮时,文本框被清除并保存空值。任何人都可以帮助我。提前感谢这里的代码:

      for (int r = 0; r < GridView2.Rows.Count; r++)
      {
        string sub_details = "";
        string remarks = "";
        GridViewRow gRow1 = GridView2.Rows[r];
        //  TextBox tb = (TextBox)gRow.Cells[2].FindControl("txt");        
        TextBox tb1 = (TextBox)gRow1.Cells[1].FindControl("txt1");
        TextBox tb2 = (TextBox)gRow1.Cells[2].FindControl("txt2");
        if (tb1 != null)
        {
            sub_details = tb1.Text;
            TextBox1.Text = sub_details;               
        }
        if (tb2 != null)
        {
           remarks= tb2.Text;
        }
        OdbcConnection DbConnection1 = new OdbcConnection(con1);
        OdbcCommand DbCommand1 = DbConnection1.CreateCommand();
        try
        {
            DbConnection1.Open();
            DbCommand1.CommandText = "insert  into tbl_campboss_report(site,tdate,entered_by,entered_time,details,camp_boss,sub_details,remarks)values('" + drpSites.SelectedItem.Text + "','" + txtDate.Text + "','" + Session["uname"].ToString() + "'," + ss + ",'" + lstDetails.SelectedItem.Text + "','" + txtCampBoss.Text + "','" + sub_details + "','" + remarks + "')";
            int t1 = DbCommand1.ExecuteNonQuery();
            if (t1 == 1)
            {
                DbConnection1.Close();
            }
        }
        catch (Exception ee)
        {
            DbConnection1.Close();
        }
    }
4

2 回答 2

2
  1. 您可以通过 template fields使用保存按钮将文本框放在页脚行中轻松完成此任务。在那里,您可以将这些值逐行保存到数据库中。
  2. 如果稍后将列添加到网格视图,使用 CellIndex 可能会失败。
  3. 您的数据库代码容易出现SQL 注入,我建议您使用参数化查询

检查以下示例

ASPX

<asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="False" 
    ShowFooter="True" EmptyDataText="&lt;h2&gt;No records found &lt;/h2&gt;" 
    onrowdeleting="gvCustomer_RowDeleting">
    <Columns>
        <asp:TemplateField HeaderText="First name">
            <FooterTemplate>
                First Name:<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFirstName" Text='<%#Bind("FirstName") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last name" >
            <FooterTemplate>
                Last Name:
                <asp:TextBox ID="txtLastName"  runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="lblLastName" Text='<%#Bind("LastName") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Favorite fruit">
            <FooterTemplate>
                Favorite fruit:
                <asp:DropDownList ID="ddlFavFruit" runat="server">
                    <asp:ListItem Text="Apple" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Mango" Value="2"></asp:ListItem>
                    <asp:ListItem Text="Orange" Value="3">Tomato</asp:ListItem>
                </asp:DropDownList>
                &nbsp;<asp:Button ID="btnSave" runat="server" Text="Save" 
                    onclick="btnSave_Click" />
            </FooterTemplate>
            <ItemTemplate>
                <asp:DropDownList ID="ddlFruits" runat="server" Enabled="False" selectedValue='<%#Bind("FruitID") %>'>
                    <asp:ListItem Text="Apple" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Mango" Value="2"></asp:ListItem>
                    <asp:ListItem Text="Orange" Value="3">Tomato</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (ViewState["myData"] == null)
                {
                    // initialize datatable
                    dt = new DataTable();
                    dt.Columns.Add(new DataColumn("Id", typeof(int)));
                    dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
                    dt.Columns.Add(new DataColumn("LastName", typeof(string)));
                    dt.Columns.Add(new DataColumn("FruitID", typeof(int)));
                    dt.Columns[0].AutoIncrement = true;
                    dt.Columns[0].AutoIncrementSeed = 1;
                    // Add sample data
                    for (int i = 0; i <= 5; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["FirstName"] = "Scott";
                        dr["LastName"] = "Tiger";
                        dr["FruitID"] = "2";
                        dt.Rows.Add(dr);
                    }
                    ViewState["myData"] = dt;
                }
                else
                {
                    dt = ViewState["myData"] as DataTable;
                }

                gvCustomer.DataSource = dt;
                gvCustomer.DataBind();
            }

        }
protected void btnSave_Click(object sender, EventArgs e)
    {
        // fetch controls from footer

        GridViewRow footerRow = ((Button)sender).NamingContainer as GridViewRow;
        if (footerRow != null)
        {
            // Fetch footer controls
            TextBox txtFirstName = footerRow.FindControl("txtFirstName") as TextBox;
            TextBox txtLastName = footerRow.FindControl("txtLastName") as TextBox;
            DropDownList ddlFruits = footerRow.FindControl("ddlFavFruit") as DropDownList;

            // Save to datatable
            dt = ViewState["myData"] as DataTable;
            DataRow dr = dt.NewRow();
            dr["FirstName"] = txtFirstName.Text.ToString();
            dr["LastName"] = txtLastName.Text.ToString();
            dr["FruitID"] = ddlFruits.SelectedValue;
            dt.Rows.Add(dr);
            gvCustomer.DataSource = dt;
            gvCustomer.DataBind();
            ViewState["myData"] = dt;

        }
    }
于 2013-05-21T07:32:07.203 回答
0

//此方法不会清除控件

//Keep this Method in Your .Aspx.cs Page
protected override void CreateChildControls()
 {
        base.CreateChildControls();
        // Keep your GridView Binding Code
 }
于 2015-02-05T17:32:23.483 回答