• GridView 添加/删除行


    .aspx

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                ForeColor="#333333" GridLines="None" ShowFooter="true" 
              onrowcommand="GridView1_RowCommand">
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:TemplateField HeaderText="姓名">
                        <ItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:TextBox>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="年龄">
                        <ItemTemplate>
                            <asp:TextBox ID="txtAge" runat="server" Width="50px" Text='<%#DataBinder.Eval(Container,"DataItem.Age") %>'></asp:TextBox>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddAge" runat="server" Width="50px" Text='<%#DataBinder.Eval(Container,"DataItem.Age") %>'></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnDel" runat="server" Text="删除" CommandName="del" CommandArgument='<%#Container.DataItemIndex %>'/>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Button ID="btnAdd" runat="server" Text="添加" CommandName="add" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </div>
        </form>
    </body>
    

    .cs

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    InitTable();
                }
            }
            private void InitTable()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Name"));
                dt.Columns.Add(new DataColumn("Age"));
                DataRow dr = dt.NewRow();
                dr["Name"] = "CY";
                dr["Age"] = "24";
                dt.Rows.Add(dr);
                ViewState["dt"] = dt;
                this.GridView1.DataSource = dt.DefaultView;
                this.GridView1.DataBind();
            }
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                DataTable dt = ViewState["dt"] as DataTable;
                if (e.CommandName == "add")
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = ((GridViewRow)((Control)e.CommandSource).Parent.Parent);
                    dr["Name"] = (row.FindControl("txtAddName") as TextBox).Text;
                    dr["Age"] = (row.FindControl("txtAddAge") as TextBox).Text;
                    dt.Rows.Add(dr);
                }
                if (e.CommandName == "del")
                {
                    object obj = e.CommandArgument;
                    int index = Convert.ToInt32(e.CommandArgument);
                    dt.Rows.RemoveAt(index);
                }
                this.GridView1.DataSource = dt.DefaultView;
                this.GridView1.DataBind();
            }
  • 相关阅读:
    闭包
    函数进阶2
    文档翻译 Python 2.7.x和Python 3.x之间的主要区别(包括示例)
    Deep Learn 关于单维度模型的多维输出
    Deep Learning 名词解释
    Deep Learning 激活函数
    Deep Learning 归一化、标准化
    Deep Learning 梯度下降
    Deep Learning 循环神经网络
    Deep Learning BP反向传播
  • 原文地址:https://www.cnblogs.com/ileaves/p/2601702.html
Copyright © 2020-2023  润新知