Geeks With Blogs

@sonamsingh
  • sonamsingh Merry Christmas dear about 1740 days ago
  • sonamsingh I am Sorry guys for that mafiawars spam.Really sorry..They spam by reading cookies too..Be careful and secure about 1812 days ago
  • sonamsingh Don't login to mafiawars on ym invite..its a spam..login if you want to..they will spam your followers too once you approve them..hello man about 1812 days ago
  • sonamsingh Hey guys,I haven't send any invitation to anybody on mafiawars family..They spammed with my twitter account..bloody hell about 1812 days ago
  • sonamsingh Any jobs for .Net Developer(c#,WPF,ASP.Net) in India ?At entry level atleast.:). Drop me a mail about 1823 days ago
  • sonamsingh I didn't ever thought that i would go on job hunting..:) Uploaded Resume to job sites about 1823 days ago

Sonam Singh

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.

 

 

 

Posted on Sunday, October 4, 2009 10:58 PM ASP.net | Back to top


Comments on this post: Adding controls dynamically in ASP.Net?

# re: Adding controls dynamically in ASP.Net?
Requesting Gravatar...
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.

Left by Jan Schepens on Oct 05, 2009 12:12 AM

# re: Adding controls dynamically in ASP.Net?
Requesting Gravatar...
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.


Left by Brian on Oct 05, 2009 4:58 AM

# re: Adding controls dynamically in ASP.Net?
Requesting Gravatar...
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
Left by Brian on Oct 05, 2009 5:13 AM

Your comment:
 (will show your gravatar)
 


Copyright © sonam | Powered by: GeeksWithBlogs.net | Join free