Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:24:38+00:00 2026-06-12T10:24:38+00:00

Alright so I need to save each input from the textbox’s in a session

  • 0

Alright so I need to save each input from the textbox’s in a session variable. The problem is that this is dynamic, meaning that the texbox ID is NatureTextbox_1, NatureTexbox_2 ect. And this makes it hard to save per session variable due to the infintite amount of texbox’s available. I have been pounding my head against the wall trying to figure this out and am resorting to being a noob and asking you guys for your advice. If you can give me any information on what to do I’d appreciate it.

This is the C# Code (Remember the textbox’s are dynamic meaning infinite):

protected void Page_Load(object sender, EventArgs e)
{
    // Add any controls that have been previously added dynamically

    for (int i = 0; i < TotalNumberAdded; ++i)
    {
        AddControls(i + 1);
    }

}
private void AddControls(int controlNumber)
{




    var newPanel = new Panel();
    var natureLabel = new Label();
    var dateLabel = new Label();
    var fatalLabel = new Label();
    var injurLabel = new Label();
    var natureTextbox = new TextBox();
    var dateTextbox = new TextBox();
    var fatalTextbox = new TextBox();
    var injurTextbox = new TextBox();

    //Validations 

    var dateRegex = new RegularExpressionValidator();

    //*****CURRENT IDEA THAT ISNT WORKING***********************************
    Session["Nature" + (TotalNumberAdded - 1)] = natureTextbox.Text.ToString(); 
    Session["Date" + (TotalNumberAdded - 1)] = dateTextbox.Text.ToString();
    Session["Fatal" + (TotalNumberAdded - 1)] = fatalTextbox.Text.ToString();
    Session["injury" + (controlNumber - 1)] = injurTextbox.Text.ToString();
    //**********************************************************************

    // textbox needs a unique id to maintain state information
    natureTextbox.ID = "NatureTextBox_" + controlNumber;
    dateTextbox.ID = "DateTextbox_" + controlNumber;
    fatalTextbox.ID = "fatalTextbox_" + controlNumber;
    injurTextbox.ID = "injurTextbox_" + controlNumber;

    natureLabel.Text = "Nature Of Accident: ";
    dateLabel.Text = "Date: ";
    fatalLabel.Text = "Fatalities: ";
    injurLabel.Text = "Injuries: ";

    dateRegex.ID = "DateRegex_" + controlNumber;
    dateRegex.Text = "Please enter in format MM/DD/YYY";
    dateRegex.ValidationExpression = @"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$";
    dateRegex.ControlToValidate = dateTextbox.ID;




    // add the label and textbox to the panel, then add the panel to the form
    newPanel.Controls.Add(new LiteralControl("<table><tr>"));
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(new LiteralControl("<td class='title-text'  >"));
    newPanel.Controls.Add(natureLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
    newPanel.Controls.Add(natureTextbox);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text' >"));
    newPanel.Controls.Add(dateLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(dateTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(dateRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr>"));

    newPanel.Controls.Add(new LiteralControl("<tr><td class='title-text'>"));
    newPanel.Controls.Add(fatalLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(fatalTextbox);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(injurLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(injurTextbox);
    newPanel.Controls.Add(new LiteralControl("</td></tr></table><br /><hr />"));
    AccidentPlaceHolder.Controls.Add(newPanel);


}
protected int TotalNumberAdded
{
    get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
    set { ViewState["TotalNumberAdded"] = value; }


}




protected void AccidentButton_Click(object sender, EventArgs e)
{
    TotalNumberAdded++;
    AddControls(TotalNumberAdded);

}
protected void PrevPage_Click(object sender, EventArgs e)
{

    Response.Redirect("employment_driversapplication_personalinfo.aspx");

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:24:39+00:00Added an answer on June 12, 2026 at 10:24 am

    This works for me, bear in mind that the values are only persisted when you click ‘add’ – so i would suggest another save button or something to persist the values when not adding a new item.

    public partial class TestRJF2 : System.Web.UI.Page
    {
        private IList<TextBox> AddedControls = new List<TextBox>();
        protected override void CreateChildControls()
        {
            BuildControls();
            base.CreateChildControls();
        }
    
        private void BuildControls()
        {
            for (var x = 0; x < TotalNumberAdded; x++)
            {
                var id = String.Format("NatureTextBox{0}", x);
                //Check if control was already added 
                //only create controls that are new for this postback
                if (AccidentPlaceHolder.FindControl(id) == null)
                {
                    var textBox = new TextBox() {ID = id};
                    AccidentPlaceHolder.Controls.Add(textBox);
                    AddedControls.Add(textBox);
                }
            }
        }
    
        protected override void OnPreRender(EventArgs e)
        {
            foreach (var ctrl in AddedControls)
            {
                var key = ctrl.ID.Replace("TextBox", String.Empty);
                Session[key] = ctrl.Text;
            }
    
            foreach (string session in Session.Keys)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session]));
            }
            base.OnPreRender(e);
        }
    
        protected void AccidentButton_Click(object sender, EventArgs e)
        {
            TotalNumberAdded++;
            BuildControls();
        } 
        protected int TotalNumberAdded
        {
            get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
            set { ViewState["TotalNumberAdded"] = value; }
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, i need to make a string defined as rootString that looks like this:
Alright so i need to open a .txt file that will be created in
Alright so I need to make a parabola that stretches across the length of
Alright, so i need to be able to match strings in ways that give
Alright... my Introduction to Data Structures from CS is so rusty I need to
Alright everyone this is a bit of a complicated setup so if I need
Alright, so heres my problem: I have an application I am working with that
Alright, so heres my problem, i need my user to be able to upload
Alright, so I have a base class which we'll call TFruit . From this
Alright I am guessing I need a subquery to solve this and I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.