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
|
No responses found. Be the first to respond and make money from revenue sharing program.
|