posts - 22, comments - 29, trackbacks - 0

My Links

News

Twitter












Archives

Post Categories

Adding controls dynamically in ASP.Net?

The Problem scenario is like this:

I have a button and a textbox on a asp.net page:

<asp:Panel ID="Panel1" runat="server"

          

                <asp:TextBox ID="GroupName" runat="server" Width="352px” />

                <asp:Button ID="CreateGroup" runat="server" Text="Create"

                onclick="CreateGroup_Click"    />

                     

 </asp:Panel>

<asp:PlaceHolder ID="PlaceHolder1" runat="server"   />

 

When I click the button a label should be added dynamically to the placeholder located in the page:

The code in click event handler is as:

protected void CreateGroup_Click(object sender, EventArgs e)

        {

         

 

            Label label = new Label();

            label.Text = GroupName.Text;

            label.ID = GroupName.Text;

            PlaceHolder1.Controls.Add(label);

        }

 

Its working but the problem is only last label are added to placeholder not the previous ones like:

If I put “Computer” in texbox and click,then the label is added .fine.

But If I put again “science” and click,then the label with “science” is added but the previous label is not rendered.

 

Any guesses,solutions?

Well..here are some workarounds:

You can use this:SEE THE EXPLANATION WHY ITS HAPPENING:

http://igregor.net/post/2007/12/24/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

Or this:

http://stackoverflow.com/questions/113392/dynamically-added-controls-in-asp-net

http://www.4guysfromrolla.com/articles/092904-1.aspx

Or the best solution is

Avoid dynamically creating controls.Actually creating controls dynamically is not the problem but maintaining there state might be.

Instead of storing dynamically added controls in ViewState,

You can use a collection to store them and store it in ViewState[Updated] . But I would again say,Avoid such things as much as possible..

If you get any better workaround ,Please do point me to that.

In my opinion,The best Solution will be the solution which will add the controls just like they are added at design time.But design time is different than runtime..Which occurs after compilation.

Please read the second comments that follows this post(Some nice cons of static variables in ASP.Net pages).

I am pasting here as i really liked the "Disadvantages of static variables in ASP.NET"

"You can use a collection to store them and declare it as static"  I need to caution against this. I have learned some hard lessons with this.

I have many developers use static variables in their ASP.NET pages and NOT realize thast every user shares that variable.

If I have static array of strings that a button click adds to, and I have 2 users click that button, there will be 1 array 2 items in that array. A page where this is the intended affect desireable is rare.. and probally badly designed :).

The implications of adding a Control as a static member would be much, much worse. (GC collection and a threading nightmare!)

Storing Meta data using a client-specific storage, like Viewstate may be the way to go.

 

 

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Sunday, October 04, 2009 10:58 PM | Filed Under [ ASP.net ]

Feedback

Gravatar

# re: Adding controls dynamically in ASP.Net?

Hi,

Could it be that your labels are all rendered, but one over the other?
Try stacking them up with absolute left and top positions, that should to the trick allright.
something with two variable x and y that increment with each control you try to render.

10/5/2009 12:12 AM | Jan Schepens
Gravatar

# re: Adding controls dynamically in ASP.Net?

Nice post, and it covers some implications of dynamic controls nicely. If you add controls to a page dynamically, be ready to develop a mechanism that will allow you to add them dynamically during page.init.

If I understand your guidance correctly, "You can use a collection to store them and declare it as static" I need to caution against this. I have learned some hard lessons with this.

I have many developers use static variables in their ASP.NET pages and NOT realize thast every user shares that variable.

If I have static array of strings that a button click adds to, and I have 2 users click that button, there will be 1 array 2 items in that array. A page where this is the intended affect desireable is rare.. and probally badly designed :).

The implications of adding a Control as a static member would be much, much worse. (GC collection and a threading nightmare!)

Storing Meta data using a client-specific storage, like Viewstate may be the way to go.


10/5/2009 4:58 AM | Brian
Gravatar

# re: Adding controls dynamically in ASP.Net?

BTW- this is an older article, but covers the infor you may be looking for:

http://www.4guysfromrolla.com/articles/092904-1.aspx

Scott Mitchell 2004. The good old days!

This plus the linked resources may be of use to your readers that are facing this issue.

And I agree., It should be easier. The othere question one needs to ask is if AJAX can provide a better, cleaner approach. We did have this option way back in 2004!

HTH
10/5/2009 5:13 AM | Brian
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: