C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Videos


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...


Birthday Greetings
Learn Windows 7: What are RSS Feeds   The RSS is a short form for Rich Site Summary is a simple format for distributing frequently updated content from a website. Most of the news websites distribute their updates in the form of RSS feeds. In this article, I have explained what is an RSS Feed.



Resources » Code Snippets » ASP.NET WebForms »

Creating Dynamic Controls, also preserve already created controls


Posted Date: 23 Apr 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Satish Kumar JMember Level: Diamond    
Rating: 1 out of 5Points: 13 (Rs 13)



Normally when you create a control dynamically means at runtime, on the next page load you will loose that control, that control will be no longer exists, in this example I am try to explain how we can store that controls and get them back.

Create a Web Application and Add one button to it.

In the page load add following code, in the following code I am creating a object of Control Collection and keeping it in the Session.


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ControlCollection ctrls = new ControlCollection(Page.Form);
Session["Controls"] = ctrls;
}
}


Now Doble click on button1 and add following code in Button1_Click event.

Here in this code I am storing all the controls created into control collection and saving into session.


protected void Button2_Click(object sender, EventArgs e)
{
//Creating object of Control Collection
ControlCollection ctrls = null;
//Checking if Object is already present in session or not
// If not present create again.
if (Session["Controls"] != null)
{
ctrls = (ControlCollection)Session["Controls"];
}
else
{
ctrls = new ControlCollection(Page.Form);
}
//Using random number to avoid conflict in control names
Random r = new Random();
//Creating label control
Label l1 = new Label();
l1.ID = "Label" + r.NextDouble().ToString();
l1.Text = "Some Text";
//Adding label control to control collection
ctrls.Add(l1);
//Creating text control
TextBox txt = new TextBox();
txt.ID = "TextBox" + r.NextDouble().ToString();
//Adding text control to control collection
ctrls.Add(txt);
//Now add controls whic are present in
//Control collection to form
foreach (Control ctrl in ctrls)
{
Page.Form.Controls.Add(ctrl);
}
//Save the control back to session.
Session["Controls"] = ctrls;
}


now we are ready with code jusr build, run and test

Hope this helps.


SatishKumar J
Microsoft MVP(ASP.NET)





Responses to the resource: "Creating Dynamic Controls, also preserve already created controls"

No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Dynamic Controls  .  Preserving Dynamic controls  .  ASP.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: restricting the textbox from autocomplete
Previous Resource: Getting the specified URL web response
Return to Resources
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



About Us    Contact Us    Privacy Policy    Terms Of Use