943,908 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 3304
  • ASP.NET RSS
Jul 2nd, 2006
0

Adding Controls Dynamically (URGENT)

Expand Post »
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:



ASP.NET Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Adzz is offline Offline
3 posts
since Jul 2006
Jul 2nd, 2006
0

Re: Adding Controls Dynamically (URGENT)

you should beable to get their values with Request.Form["fieldName"]
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jul 3rd, 2006
0

Re: Adding Controls Dynamically (URGENT)

thanks for your help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Adzz is offline Offline
3 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: username after /
Next Thread in ASP.NET Forum Timeline: active row





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC