点击按钮动态添加新的一行(添加文本框)

类似的论坛上发表投票主题:

 ※ <wbr>点击按钮动态添加新的一行(添加文本框)

 

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DataList ID="DataList1" runat="server" Width="100%" BorderWidth="1" ondeletecommand="MinusItemList_DeleteCommand">
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%#("OptionName")%>'></asp:TextBox>
            <asp:Button ID="Button1" runat="server" CommandName="delete" Text="删除" />
        </ItemTemplate>
    </asp:DataList>
    <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="添加项" />
    <asp:Button ID="Button3" runat="server" Text="保存" οnclick="Button3_Click" />
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </form>
</body>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebCommonLib.DataList
{
    public partial class Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("OptionName", typeof(string));
                for (int i = 0; i < 5; i++)
                {
                    DataRow row = table.NewRow();
                    row["OptionName"] = string.Empty;
                    table.Rows.Add(row);
                }

                ViewState["table"] = table;
                DataList1.DataSource = table;
                DataList1.DataBind();
            }
        }

        protected void MinusItemList_DeleteCommand(object source, DataListCommandEventArgs e)
        {  
            foreach (DataListItem item in DataList1.Items)
            {
                Table.Rows[item.ItemIndex]["OptionName"] = (item.Controls[1] as TextBox).Text;
            }

            Table.Rows.RemoveAt(e.Item.ItemIndex);
            DataList1.DataSource = Table;
            DataList1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            DataRow row = Table.NewRow();
            Table.Rows.Add(row);
            DataList1.DataSource = Table;
            DataList1.DataBind();
        }
       
        private DataTable Table {
            get {
                if (ViewState["table"] != null)
                    return (DataTable)ViewState["table"];
                return null;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in DataList1.Items)
            {
                Literal1.Text += (item.Controls[1] as TextBox).Text;
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值