Maintain control values after postback

Joined
Jul 4, 2006
Messages
1
Reaction score
0
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 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:


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;


public partial class questions : 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 the values the user enterd
        /*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);
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top