943,737 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 27515
  • ASP.NET RSS
Jul 4th, 2006
0

Maintain control Values after PostBack

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

 
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Adzz is offline Offline
3 posts
since Jul 2006
Jul 6th, 2006
0

Re: Maintain control Values after PostBack

Hmm, i replyed to a post exactly like this somewhere else on this forum.
Anyways use Request.Form["controlID"] to get the value after a postback.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Feb 10th, 2010
0
Re: Maintain control Values after PostBack
Thank you for the post.I am still facing some problems.Onclick of a link button I am generating a row of the table containing 4 controls dynamically.I am able to generate two rows of controls dynamically but when I click on the Add link button which is created dynamically for the thrid time, I am getting commas in the textboxes of firstrow.

pls suggest me a solution for this
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swati.mehta is offline Offline
1 posts
since Feb 2010
Feb 10th, 2010
0
Re: Maintain control Values after PostBack
It is Simple. Add the grid and the Button inside an Update Panel and every time you click the Button you will not loose the values of the controls that you created dynamically.


kind Regards

Vuyiswa Maseko
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007

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: Picture viewer
Next Thread in ASP.NET Forum Timeline: How to resolve Tiny Mce display and save problems in Firefox





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


Follow us on Twitter


© 2011 DaniWeb® LLC