none
ASP.net怎么动态添加表格? RRS feed

答案

  • 一个例子:

    aspx页面:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="gridView1_OnRowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblID" Text='<% #DataBinder.Eval(Container.DataItem,"ID") %>' Visible="false"></asp:Label>
                        abc:<asp:TextBox runat="server" ID="txtAbc" Text='<% #DataBinder.Eval(Container.DataItem,"abc") %>'></asp:TextBox>
                        <asp:Button runat="server" ID="btnDelete" Text="Delete" CommandArgument='<% #DataBinder.Eval(Container.DataItem,"ID") %>' CommandName="DeleteData"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button runat="server" ID="btnAdd" Text="Add" OnClick="btnAdd_Click" />

    cs代码:

            DataTable dataTable = new DataTable();
            DataRow dataRow;
            DataRow[] filterByIDRows;

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GenerateDataTable();
                    GridView1.DataSource = dataTable;
                    GridView1.DataBind();
                    Session["dt"] = dataTable;
                }
            }

            protected void GenerateDataTable()
            {
                DataColumn dataColumn;
                dataColumn = new DataColumn("ID", Type.GetType("System.Int32"));
                dataColumn.AutoIncrement = true;
                dataTable.Columns.Add(dataColumn);

                dataColumn= new DataColumn("abc");
                dataTable.Columns.Add(dataColumn);

                dataColumn = new DataColumn("bcd");
                dataTable.Columns.Add(dataColumn);

                dataRow = dataTable.NewRow();
                dataTable.Rows.Add(dataRow);
            }

            protected void Refresh()
            {
                GridView1.DataSource = dataTable;
                GridView1.DataBind();
            }

            protected void btnAdd_Click(object sender, EventArgs e)
            {
                Save();
                dataRow = dataTable.NewRow();
                dataTable.Rows.Add(dataRow);
                Refresh();
            }

            protected void Save()
            {
                int id;

                dataTable = Session["dt"] as DataTable;
                foreach (GridViewRow gridViewRow in GridView1.Rows)
                {
                    id = int.Parse((gridViewRow.FindControl("lblID") as Label).Text);
                    filterByIDRows = dataTable.Select("ID = " + id);
                    filterByIDRows[0]["abc"] = (gridViewRow.FindControl("txtAbc") as TextBox).Text;
                }
            }

            protected void gridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
            {
                int id;

                id = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName)
                {
                    case "DeleteData":
                        Delete(id);
                        Refresh();
                        break;
                }
            }

            protected void Delete(int id)
            {
                dataTable = Session["dt"] as DataTable;
                filterByIDRows = dataTable.Select("ID = " + id);

                dataTable.Rows.Remove(filterByIDRows[0]);
            }

     

    试试看,是不是你要的效果

    • 已标记为答案 小米哦 2011年5月12日 7:10
    2011年5月12日 7:05