| | |
Maintain control Values after PostBack
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 3
Reputation:
Solved Threads: 0
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 lost 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 are lost after post back.
Here is my code:
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 are lost after post back.
Here is my code:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; publicpartialclassquestions : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState.Add("mode", "0"); } } protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); if (ViewState["mode"].ToString() == "1") { if (TextBox1.Text != "") { for (int i = 0; i < int.Parse(TextBox1.Text); i++) { CreateControls(i); } } } } ///<summary> /// Calls the function "CreateControls" which creates a number of controls based on the number eneterd in the TextBox. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> protected void BTN_Submit_Click(object sender, EventArgs e) { if (TextBox1.Text != "") { for (int i = 0; i < int.Parse(TextBox1.Text); i++) { CreateControls(i); } } } ///<summary> /// Checks the values entered by the user and calculates the total result. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> protected void BTN_Result_Click(object sender, EventArgs e) { if (TextBox1.Text != "") { for (int i = 0; i < int.Parse(TextBox1.Text); i++) { CreateControls(i); } } ViewState.Add("mode", "1"); //check results of entered by user /*int result = 0; for (int i = 0; i < int.Parse(TextBox1.Text); i++) { bool ans = ((ASP.mcq_ascx)(this.Q_PlaceHolder.FindControl("newc" + i))).CheckAnswer(); if (ans) { result++; } } Response.Write(result);*/ } ///<summary> /// Creates controls dynamically to the page. ///</summary> protected void CreateControls(int i) { ASP.mcq_ascx newc = new ASP.mcq_ascx(); newc.ID = "newc" + i.ToString(); this.Q_PlaceHolder.Controls.Add(newc); } }
![]() |
Similar Threads
- ABCs OF BEING A FATHER (Geeks' Lounge)
- audio level detection (Visual Basic 4 / 5 / 6)
- C# Classes (C#)
- Identity Property problem (MS SQL)
- how do i update to Access db? (C#)
- Community with forum, blogs and articles (Growing an Online Community)
- Hello everybody (Community Introductions)
- ASP.NET / C# Dynamic Control (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Hello
- Next Thread: Batch file in ASP.Net to convert Excel files into higher version
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# c#gridviewcolumn cac checkbox click commonfunctions compatible confirmationcodegeneration content courier css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dialog dropdownlist dynamically edit fileuploader fill flash formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers





