C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Adding Controls Dynamically in ASP.NET


Posted Date: 07 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: shakti singh tanwarMember Level: Diamond    
Rating: Points: 5



The code sample illustrates how to add controls to a form dynamically and access values associated with them.

Place a Placeholder control on your form with id, say, PlaceHolder1.
Then add this code


TextBox txt = new TextBox();
txt.Id ="txt1";
txt.Text="something;
PlaceHolder1.Controls.Add( txt); // adding controls dynamically


Similarly add a DropDownList

To get value entered by user in this dynamic control at runtime.


foreach ( Control ctrl in PlaceHolder1.Controls )
{
if ( ctrl is TextBox )
{
TextBox txt = (TextBox)ctrl;
//accessing values of dynamically created controls
string textBoxVal = txt.Text;
}
}




Responses

Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 0
good,


Author: Ashish verma    27 Jun 2008Member Level: Bronze   Points : 1
Its a nice piece of coding.
Keep posting.
Thanks.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Creating controls dynamically  .  Adding controls dynamically  .  Accessing control's values dynamically  .  

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: Three Tier Architecture
Previous Resource: Content Transfer between two Listboxes
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use