Adding Controls Dynamically (URGENT)

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 3
Reputation: Adzz is an unknown quantity at this point 
Solved Threads: 0
Adzz Adzz is offline Offline
Newbie Poster

Adding Controls Dynamically (URGENT)

 
0
  #1
Jul 2nd, 2006
I have to create a number of controls equal to a number entered in a textbox by the user. I created a web user control, which has a radio button list. Then when the user press a Submit button, I want to check the selected values of the controls created dynamically, but the problem is that they are removed on post-back.

I managed with the aid of an article I read on the net (http://www.tgreer.com/aspnet_html_04.html), to maintain the controls created dynamically after post back, but still one problem I have is the values the user enters in the dynamically created controls is lost after post back.

Here is my code:



  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12.  
  13. public partial class questions : System.Web.UI.Page
  14. {
  15. void Page_Load(object sender, EventArgs e)
  16. {
  17. if (!=IsPostBack)
  18. {
  19. ViewState.Add("mode", "0");
  20. }
  21. }
  22. protected override void LoadViewState(object savedState)
  23. {
  24. base.LoadViewState(savedState);
  25. if (ViewState["mode"].ToString() == "1")
  26. {
  27. CreateControls();
  28. }
  29. }
  30. protected void BTN_Submit_Click(object sender, EventArgs e)
  31. {
  32. if (TextBox1.Text != "")
  33. {
  34. for (int i = 0; i < int.Parse(TextBox1.Text); i++)
  35. {
  36. ASP.mcq_ascx newc = new ASP.mcq_ascx();
  37. newc.ID = "newc" + i.ToString();
  38. this.Q_PlaceHolder.Controls.Add(newc);
  39. }
  40. }
  41. }
  42. protected void BTN_Result_Click(object sender, EventArgs e)
  43. {
  44. CreateControls();
  45. ViewState.Add("mode", "1");
  46. }
  47. protected void CreateControls()
  48. {
  49. if (TextBox1.Text != "")
  50. {
  51. for (int i = 0; i < int.Parse(TextBox1.Text); i++)
  52. {
  53. ASP.mcq_ascx newc = new ASP.mcq_ascx();
  54. newc.ID = "newc" + i.ToString();
  55. this.Q_PlaceHolder.Controls.Add(newc);
  56. }
  57. }
  58. }
  59. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Adding Controls Dynamically (URGENT)

 
0
  #2
Jul 2nd, 2006
you should beable to get their values with Request.Form["fieldName"]
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 3
Reputation: Adzz is an unknown quantity at this point 
Solved Threads: 0
Adzz Adzz is offline Offline
Newbie Poster

Re: Adding Controls Dynamically (URGENT)

 
0
  #3
Jul 3rd, 2006
thanks for your help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC