Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>添加多条数据实例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="text-align: center">
            <table style="width: 500px">
                <tr>
                    <td colspan="2" style="height: 40px">
                        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="新宋体" ForeColor="#FFC080"
                            Height="20px" Text="添加多条数据" Width="184px"></asp:Label></td>
                </tr>
                <tr>
                    <td colspan="2" style="height: 31px">
                        <asp:Table ID="Table1" runat="server" Height="16px" Width="165px">
                        </asp:Table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnAddTxt" runat="server" Font-Size="9pt" OnClick="Button1_Click"
                            Text="添加单元格" /><asp:Button ID="btnData" runat="server" Font-Size="9pt" OnClick="btnData_Click"
                                Text="添加数据" /></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" ForeColor="#333333"
                            GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" Width="261px" Font-Size="Smaller">
                            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.bindGridView();
        if (ViewState["Count"] != null)
        {
            for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
            {
                AddTextBox();//调用该自定义方法动态生成表格
            }
        }
        else
        {
            this.btnData.Enabled = false;
        }
    }
    public void bindGridView()
    {
        string ConStr = ConfigurationSettings.AppSettings["strCon"];
        string cmdtxt = "SELECT * FROM tb_USER";
        SqlConnection Con = new SqlConnection(ConStr);
        SqlDataAdapter da = new SqlDataAdapter(cmdtxt, Con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();
    }
    //向页面中添加文本框
    public void AddTextBox()
    {
        TableRow tr = new TableRow();//创建表格行对象
        TableCell tc1 = new TableCell();//创建表格中单元格对象
        TextBox txt = new TextBox();//创建文本框对象
        txt.ID = "tb" + Table1.Rows.Count;
        txt.Font.Size = FontUnit.Point(9);
        TableCell tc2 = new TableCell();
        Label lab = new Label();
        lab.ID = "lab" + Table1.Rows.Count;
        lab.Width = 180;
        lab.Text = "添加的学生姓名数据" + (Table1.Rows.Count + 1);
        tc2.Controls.Add(lab);//动态添加Label标签
        tc1.Controls.Add(txt);//动态添加文本框
        tr.Cells.Add(tc2);
        tr.Cells.Add(tc1);
        Table1.Rows.Add(tr);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //动态添加控件
        AddTextBox();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
        this.btnData.Enabled = true;
    }

    protected void btnData_Click(object sender, EventArgs e)
    {
        string ConStr = ConfigurationSettings.AppSettings["strCon"];
        SqlConnection Con = new SqlConnection(ConStr);
        Con.Open();
        SqlCommand Com;
        string cmdtxt = String.Empty;
        //将文本框中的内容循环插入到数据库中
        for (int i = 0; i < Table1.Rows.Count; i++)
        {
            TextBox txt = new TextBox();
            cmdtxt = "INSERT INTO tb_User(userName) VALUES('" + ((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + "')";
            Com = new SqlCommand(cmdtxt, Con);
            Com.ExecuteNonQuery();
            Response.Write("<script>alert('恭喜您,数据插入成功!');location='Default.aspx'</script>");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.bindGridView();
    }
}

 

posted on 2013-03-11 01:49  松竹梅  阅读(317)  评论(0编辑  收藏  举报