【问题标题】:Button click event works only once asp.net按钮单击事件仅在 asp.net 中有效一次
【发布时间】:2016-03-20 09:03:41
【问题描述】:

这是我的 .aspx 代码:

<asp:Panel ID="passengerBox" CssClass="transparentBox" runat="server">
    <asp:Table ID="passengerDetailsTable" runat="server">
        <asp:TableRow>
            <asp:TableCell><asp:Label runat="server" Text="1." /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="name1" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="age1" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
            <asp:TableCell><asp:Label runat="server" Text="2." /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="name2" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="age2" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
            <asp:TableCell><asp:Label runat="server" Text="3." /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="name3" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="age3" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    <asp:Button ID="addPerson" runat="server" Text="Add Person" OnClick="addPerson_Click" />
</asp:Panel>

这是我的 .aspx.cs 页面:

public partial class Pages_Passengers : System.Web.UI.Page
{
    int count = 3;
    protected void addPerson_Click(object sender, EventArgs e)
    {
        TextBox nameTxtBox = new TextBox();
        TextBox ageTxtBox = new TextBox();

        TableRow tRow = new TableRow();
        for (int i = 1; i <= 3; i++)
        {
            TableCell tCell = new TableCell();
            if (i == 1)
                tCell.Text = count.ToString() + ".";
            if (i == 2)
                tCell.Controls.Add(nameTxtBox);
            if (i == 3)
                tCell.Controls.Add(ageTxtBox);
            tRow.Cells.Add(tCell);
        }
        passengerDetailsTable.Rows.Add(tRow);
    }
}

每次单击“addPerson”按钮时,我都想在表格中添加一个新行。但是这个按钮只能工作一次。我该怎么办?

【问题讨论】:

    标签: asp.net button click row


    【解决方案1】:

    好的!我假设代码没有真正的目的,只是用于培训。

    如果您不使用数据库,获得数据持久性的简单方法是使用会话变量(所以 .. 它有效,但让我提醒您这并不总是一个好主意)

    网络表单:

    <body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
    

    您的网络表单现在更干净了,因为表格将由后面的代码管理。

    代码:

    public partial class WebForm1 : System.Web.UI.Page
    {
        private Table _passengerDetailsTable;
        private int _rowCounter = 0;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            // a recomandation ... make strong use of comments 
    
            this.InitTable(); // initializing the table from code behind 
    
            this._rowCounter = this._passengerDetailsTable.Rows.Count;
        }
    
        private void InitTable()
        {
            this._passengerDetailsTable = new Table();
    
            if (Page.IsPostBack) // ok! if page is loaded for the first time we can create table, if we are back by a summit button code will be skipped
            {
                this._passengerDetailsTable = (Table)Session["tblPassengers"]; // get back the Table we stored in a session variable 
            }
            else
            {
                TableRow dr = CreatePassengerDataRow(); // remember DRY Don't Repeat Yourself you are expected to create many rows. Bundle all the needed stuff inside a method 
    
                // perhaps you should create an header for the table to enhance readibility
    
                this._passengerDetailsTable.Rows.Add(dr); // add the datarow    
    
                Session["tblPassengers"] = this._passengerDetailsTable; // store the table state in a session variable 
            }
    
    
            this.form1.Controls.Add(_passengerDetailsTable); // add the control to the form             
        }
    
        /// <summary>
        /// create a new datarow 
        /// </summary>
        /// <param name="counter"></param>
        /// <returns></returns>
        private TableRow CreatePassengerDataRow()
        {
    
            var dr = new TableRow(); // keeping things easy I'll create a single TableRow               
    
            // avoid the loop, it make code less readable and has no clear purpose 
    
            var tc0 = new TableCell();  // counter cell
            tc0.Text = String.Format("#{0}", (this._rowCounter));
            dr.Cells.Add(tc0);
    
            var tc1 = new TableCell(); // name cell
            tc1.Controls.Add(this.CreateInputTb("name", this._rowCounter)); // DRY again 
            dr.Cells.Add(tc1);
    
            var tc2 = new TableCell(); // age cell 
            tc2.Controls.Add(this.CreateInputTb("age", this._rowCounter));
            dr.Cells.Add(tc2);
            return dr;
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="counter"></param>
        /// <returns></returns>
        private TextBox CreateInputTb(string name, int counter)
        {
            var textBox = new TextBox();
            textBox.ID = String.Format("{0}_{1}", name, counter); // create a tb unique identifier (makes your life easier if you plan to use is data)
            return textBox;
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            TableRow dataRow = this.CreatePassengerDataRow(); // create a brand new role 
            this._passengerDetailsTable.Rows.Add(dataRow); // append the role 
        }
    }
    

    享受吧。

    【讨论】:

    • 那么当我再次点击按钮时,如何让添加的行保留在页面上??
    • 仅当 Page.IsPostBack 为 false 时才使用 onload 事件从后面的代码初始化表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多