2016-03-20 10 views
1

Das ist mein aspx-Code ist:Button-Click-Ereignis funktioniert nur einmal asp.net

<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> 

Dies ist meine aspx.cs Seite:

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); 
    } 
} 

Ich möchte eine neue Zeile hinzufügen die Tabelle jedes Mal, wenn ich auf den Button "addPerson" klicke. Aber dieser Knopf funktioniert nur einmal. Was mache ich?

Antwort

0

Ok! Ich gehe davon aus, dass der Code keinen wirklichen Zweck hat, aber nur für das Training.

Wenn Sie nicht Datenbanken die einfache Möglichkeit, mit Datenpersistenz zu bekommen, ist Session-Variablen verwenden (so .. es mich aber Sie funktioniert lassen erinnern, es ist nicht immer eine gute Idee)

Web-Formular:

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

Ihr Webformular jetzt ist sauberer, weil der Tisch hinter von Code verwaltet wird.

Der Code:

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 
    } 
} 

Genießen.

+0

Also wie behalte ich die hinzugefügten Reihen bleiben auf der Seite, wenn ich den Knopf wieder klicke ?? –

+0

Initialisieren Sie die Tabelle aus Code mit dem Onload-Ereignis nur dann, wenn Page.IsPostBack false –

+0

Können Sie mir bitte durch Code zeigen ?? –

Verwandte Themen