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- 2


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



About this code



This code is based on the HTML part that we discussed in last article. You can modify the events & Control names according to you.




This the Page load event. In the Page_Load method, we rebuilds all the product fields ever time the page is loaded. I have explained the reason of this in my previous article.


private void Page_Load(object sender, System.EventArgs e)
{
int counter;
if(!IsPostBack)
ViewState["ProductCount"] = 0;
else if(Convert.ToInt32(ViewState["ProductCount"]) > 1)
{
for(counter=1;counter <= Convert.ToInt32(ViewState["ProductCount"]);counter++)
AddProduct(counter);
}
}







This is the method that we have used to add the controls.


private void AddProduct(int intFieldNum)
{
LiteralControl litLabel = new LiteralControl();
litLabel.Text = "Product " + intFieldNum.ToString() ;
PlaceHolder1.Controls.Add(litLabel);

TextBox txtField = new TextBox();
txtField.ID = "txtProduct" + intFieldNum.ToString();
PlaceHolder1.Controls.Add(txtField);
}









private void btnAdd_Click(object sender, System.EventArgs e)
{
int intCounter = Int32.Parse(TextBox1.Text);
for(int i = 1;i <= intCounter;i++ )
{
ViewState["ProductCount"] = Convert.ToInt32(ViewState["ProductCount"]) + 1;
AddProduct(Convert.ToInt32(ViewState["ProductCount"]));
}
}









private void btnShow_Click(object sender, System.EventArgs e)
{
foreach(Control ctrlControl in PlaceHolder1.Controls)
{
if(ctrlControl is TextBox)
{
TextBox txt = (TextBox)ctrlControl;
lblMessage.Text = lblMessage.Text + txt.Text + "<br>";
}
}
}





I hope now you have a good idea of this process.
For any query, mail me at:
rathi.del@gmail.com



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- 2  .  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: Add controls at Run Time Part 1
Previous Resource: Components in CLR
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