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 6957681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:02:15+00:00 2026-05-27T15:02:15+00:00

Within an asp.net web form I would like to add values back to a

  • 0

Within an asp.net web form I would like to add values back to a .net object type that I have created on the server within my code behind I have the following:

  protected void Page_Load (object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                myArrayList.Add("Value 1");
                myArrayList.Add("Value 2");
                myBox.DataSource = myArrayList;
            }
            myBox.DataBind();
            myTime.Text = DateTime.Now.ToString();
        }

        protected void btnAddText_Click (object sender, EventArgs e)
        {
            myArrayList.Add(mytext.Text.ToString());
            myBox.DataSource = myArrayList;
            myBox.DataBind();
        }

        public ArrayList myArrayList = new ArrayList();

What I would like to do is take values entered into the textbox on the client and when the button is clicked add/append them to the array. Currently, (as expected) the array is reset to null once the button is clicked. If I don’t use the isPostBack it only retains the last value, once again as expected. I have asp:updatepanels in place in the Ui with conditional updates and I realize that the asp.net page life-cycle still fires on the postback (causing Init, Load, pre-render and unload).

My asp.net is rusty but for scenarios like this is the best (only) approach to use Session State and store the array in the session for manipulation or am I missing something?

Thanks for any guidance,

  • 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-05-27T15:02:16+00:00Added an answer on May 27, 2026 at 3:02 pm

    After some more research I decided to go the route of using Session State. There are a number of options for state management but session was the most straight forward and extensible for what I wanted to do.

    For any interested here is a working solution showing a list array being used as a session variable.

    Within a document I setup a basic field, button and listbox for testing:

     <table>
                <tr>
                    <td>
                        <asp:ListBox ID="lstItems" runat="server" Width="200px" Height="120px" />
                    </td>
                    <td valign="top">
                        <asp:TextBox ID="myTextBox" Height="32px" runat="server" Width="200px" />&nbsp;&nbsp;<asp:Button
                            Text="Add User" Width="80px" Height="32px" ID="myButton" OnClick="AddNames" runat="server" />
                    </td>
                </tr>
            </table>
    

    Then within the code behind I setup the following:

    //quick example of session state where we contribute to the session object from the Ui
    //similar to a shoping cart example
    // You could use any object or type I just used an array as it was fast

    protected void Page_Load (object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Create the session variable and base it upon the array created ( this can be any type)
            Session["myList"] = mainList;
        }
    }
    
    protected void AddNames(object sender, EventArgs e)
    {
        //Here I set the array to = the Session. key point here is a session object must be cast to the approriate type on assignement
        mainList = (ArrayList)Session["myList"];
        //add to the array
        mainList.Add(myTextBox.Text.ToString());
        //bind to UI
        lstItems.DataSource = mainList;
        lstItems.DataBind();
        myTextBox.Text = string.Empty;
    }
    
    ArrayList mainList = new ArrayList();      
    

    When working with session variables it may also be a good idea to test for null session variable (to see if the variable exits). In some instances failure to do so could result in a null reference exception being thrown if you attempt to add to the session before it is created.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a silverlight application that is hosted within a asp.net web application. in
Within a C# / ASP.NET MVC web app, I would like to display HTML
I have an asp.net web project that references a domain project. From within the
I have an asp.net web app that is using an iFrame (called fraContent) within
I have an ASP.NET web page that has an advanced search tab. Within this
I have an ASP.NET web form that has a container usercontrol that hosts several
I have an ASP.NET page that has three div s within the only form
I have a form within an ASP.NET MVC application and I'm trying to submit
within an asp.net webform project I have a session variable that I am populating
I use asp.net + MVC1.0 and in a web-page I have several forms, within

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.