C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Add controls at Run Time Part 1


Posted Date: 10 Oct 2006    Resource Type: Articles    Category: .NET Framework
Author: Anuj RathiMember Level: Gold    
Rating: 1 out of 5Points: 10



Introduction


There are so many situations in which we want to add controls at run time because we do'nt know the Exact number of controls that we have to add.

eg. If we have a form in which we want to some product name & these products numbers may be different for different users.
One another example - In a registration form, we want to give a choice to enter the number of emails that a user can enter in different TextBoxes.
So we can'nt decide the number of controls at design time. It will be better to add these controls at run time.

The Controls Collection


Every control has a "Controls" that represants its child controls. If we want to display all the controls in a Page, then we can use

" Page.Controls "

recursively to find all the controls present in a form. So we can also add controls to this collection.
But If we want to add Controls at run time then It will be better to use a PlaceHolder control or Panel.

Dyamically creating controls



When we create controls dynamically, we should remember that any control that will create at run time, will exist untill our page is exists in mamory means if we move from one page to another then all controls that we created in First form, will be lost because when we create controls dynamically, the control is not automatically retained between form posts. Means the control itself is not preserverd in the Page's ViewState. However, any TEXT we enter into dynamically created control fields automatically preserved in ViewState.



An Example


Suppose we want to add some text boxes at run time.

The HTML part of our example given below,Paste it in the body section of your .aspx page.




<body >
<form id="Form1" method="post" runat="server" autocomplete="off" >
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="50%" border="1" >
<TR >
<TD style="HEIGHT: 20px" width="20%" >Enter number of fields: </TD >
<TD style="HEIGHT: 20px" width="30%" >
<asp:TextBox id="TextBox1" runat="server" Width="48px" > </asp:TextBox > </TD >
</TR >
<TR >
<TD > <asp:Button id="btnShow" runat="server" Text="Show All Message" > </asp:Button > </TD >
<TD > <asp:Button id="btnAdd" runat="server" Text="Add" > </asp:Button > </TD >
</TR >
<TR >
<TD colSpan="2" > <asp:PlaceHolder id="PlaceHolder1" runat="server" > </asp:PlaceHolder > </TD >
</TR >
<TR >
<TD > <asp:Label id="lblMessage" runat="server" > </asp:Label > </TD >
<td align="right" > <a href="WebForm1.aspx" >First Example </a > </td >
</TR >
</TABLE >
</form >
</body >




The code part we will see in the next article..




Responses


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.
Run Time  .  Part- 1  .  Controls  .  Add  .  

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: Visual Studio 2005 and Web References
Previous Resource: Add controls at Run Time Part- 2
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use