How to add controls dynamically when a button is clicked in asp.net?

I am trying to add controls dynamically

code:

AddVisaControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %> <%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %> <div id="divreg" runat="server"> <table id="tbl" runat="server"> <tr> <td class="style8"> Visa Number:</td> <td class="style20"><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td> <td class="style22"> Country Name:</td> <td class="style23"> <asp:DropDownList ID="dropCountry" Width="165px" runat="server"> </asp:DropDownList></td> </tr> <tr> <td class="style22"> Type of Visa:</td> <td class="style23"> <asp:DropDownList ID="dropVisa" Width="165px" runat="server"> </asp:DropDownList></td> <td class="style22"> Type of Entry:</td> <td class="style23"> <asp:DropDownList ID="dropEntry" Width="165px" runat="server"> </asp:DropDownList></td> </tr> <tr> <td class="style8">&nbsp; Expiry Date</td> <td class="style20"> </td> </tr> </table> 

.cs code:

Below code is the problem, the first time I click the add button, it correctly adds the controls, but if I close the browser window and start up again, and click the add button, I got more controls

  static int i = 0; protected void addnewtext_Click(object sender, EventArgs e) { i++; for (int j = 0; j <= i; j++) { AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx"); PlaceHolder1.Controls.Add(ac); PlaceHolder1.Controls.Add(new LiteralControl("<BR>")); } } 

In the image below, if I click the add additional visa button, I want to get other visa information

enter image description here

Any ideas? thanks in advance

+6
source share
3 answers

I will show some examples that you can try in your own way.

The idea would be to create a list of buttons in which you would save the buttons created in btnCreateDynamic_click

you may have a method like this:

 private Button CreateButton(string id, string name) { Button b = new Button(); b.Text = name; b.ID = id; b.Click += new EventHandler(Button_Click); b.OnClientClick = "ButtonClick('" + b.ClientID + "')"; return b; } 

in btnCreateDynamic_click you might have something like:

 Button b = CreateButton("dinamicBtn"+myDinamicButtonsList.Count.ToString(),"dinamicBtn"+myDinamicButtonsList.Count.ToString()); myDinamicButtonsList.add(b); and in the pageLoad for example you could do something like foreach(button btn in myDinamicButtonsList){ form1.Controls.Add(btn)); } List<Button> myDinamicButtonsList = new List<Button>(); 

myDinamicButtonsList should be stored somewhere from where it can be obtained after each request.

EDIT: when loading the page, you might have something like this:

 if(Session["myDinamicButtons"] == null){ List<Button> myDinamicButtonsList = new List<Button>(); Session["myDinamicButtons"] = myDinamicButtonsList; } foreach(Button btn in Session["myDinamicButtons"] as List<Button>){ form1.Controls.Add(btn)); } 

I have not tested it, but it should work.

also adds some information, the following may help.

Your event of clicking a button on the client will cause a postback to the page that will start the life cycle of the ASP.Net page. enter image description here

Your button click event on the server is PostBackEvent , and you must use the same CreateMyButton() call method that you used in the Load or Init events.

+13
source

if you remove the static variable "i" and use hidenInput to support the absence of the generated controls (or session values), you'll be fine.

But I suggest you read the following article to find the best way to create dynamic controls:

Creating Dynamic Management in ASP.NET

+1
source

Remove static int i = 0;

Change the method as shown below

  protected void addnewtext_Click(object sender, EventArgs e) { AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx"); PlaceHolder1.Controls.Add(ac); PlaceHolder1.Controls.Add(new LiteralControl("<BR>")); } 
0
source

Source: https://habr.com/ru/post/954242/


All Articles