c# - Adding controls in runtime problem ASP.Net - Stack Overflow

I put a link on a page and onclick i wrote some code to add some controls after clicking,DropDownList n

I put a link on a page and onclick i wrote some code to add some controls after clicking,

    DropDownList newDropdownlist = new DropDownList();
    panel.Controls.Add(newDropdownlist);
    CheckBox newChkbox = new CheckBox();
    panel.Controls.Add(newChkbox);
    TextBox txt = new TextBox();
    txt.ID = "txtPhoneValue";
    panel.Controls.Add(txt);

My Problem is when i click on this link it add these controls one time, but if i clicked again it don't add more, seams its removing the previous added controls and re add them again.

I want to add more and more each link click.

I put a link on a page and onclick i wrote some code to add some controls after clicking,

    DropDownList newDropdownlist = new DropDownList();
    panel.Controls.Add(newDropdownlist);
    CheckBox newChkbox = new CheckBox();
    panel.Controls.Add(newChkbox);
    TextBox txt = new TextBox();
    txt.ID = "txtPhoneValue";
    panel.Controls.Add(txt);

My Problem is when i click on this link it add these controls one time, but if i clicked again it don't add more, seams its removing the previous added controls and re add them again.

I want to add more and more each link click.

Share edited Feb 24, 2009 at 10:59 Amr Elgarhy asked Feb 24, 2009 at 10:45 Amr ElgarhyAmr Elgarhy 69k70 gold badges192 silver badges310 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Ok, these added controls are not persisted anywhere between postbacks. So, you should add them every time the page reloads.

Consider using some flags (stored in the Session for example) to indicate that additional controls must be added.

Your controls're disappearing, as they're not stored anywhere (the page forgets about them on postback). Remember that, on each postback, your page has to be recreated.

There're quite a few good articles about working with dynamically created controls. Also to fully understand what the problem is, it's neccessary to familiarize yourself with the page lifecycle.

Here're two articles that really helped me:

  • Working with dynamically created controls

  • Truly understanding viewstate (this's brilliant)

You need to re-create the controls (with the same IDs!) on post-back, you can do this in the CreateChildControls method.

It's worth looking at the Page Life-Cycle of ASP.NET to understand when and where stuff can be modified. If it's to late, it won't get added to the ViewState etc, so it's worth understanding especially when using dynamically created controls.

How is this link construct??

If your link control is an html control (client control) the behaviour you're experienced is right.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744932047a4601798.html

相关推荐

  • c# - Adding controls in runtime problem ASP.Net - Stack Overflow

    I put a link on a page and onclick i wrote some code to add some controls after clicking,DropDownList n

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信