7

I'm 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 when the first time I click add button it is adding controls properly but If I close the browser window and comes 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 below image If I click add more visa button I want to get another visa details

enter image description here

Any ideas? Thanks in advance

4
  • naturally , you have kept your integer variable 'i' static Commented Sep 20, 2013 at 10:30
  • Definitely clicking the Add button will add more controls as per your code logic. So what exactly are you looking for?? Should the Add button not add more controls ?? Commented Sep 20, 2013 at 10:32
  • Exactly that is the problem then how to solve that? Commented Sep 20, 2013 at 10:32
  • If I add two times then I close the browser then again open add the controls means it is adding more than i.e(previously added controls) Commented Sep 20, 2013 at 10:40

3 Answers 3

10

I will show one examples you can try it your own way

An idea would be to create a list of buttons in which you'd store the buttons you created inbtnCreateDynamic_click

you could have a method like:

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 could 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 could be retrieved after each request.

EDIT: In page load you could 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 didn't tested it but it should work.

also put on some information following may more help..

Your button click event at the client will cause a page postback that will start the ASP.Net Page Life-cycle. enter image description here

Your button click event on the server is a PostBackEvent and you should be able to use the same method call CreateMyButton() that you used in the Load or Init events.

Sign up to request clarification or add additional context in comments.

Comments

1

if you remove the static 'i' variable and use a hidenInput to maintain the no of created controls (or a session value) you will be fine.

But I suggest you read the following article to find a better way of creating dynamic controls :

Dynamic control creation in ASP.NET

Comments

0

Remove static int i = 0;

Change the method as 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>"));

    }

2 Comments

If I use like this controls are overwriting
you are using lot of css styles , so something is acting absolute (margins,padding ... ) not sure . But they have to be relative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.