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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:52:05+00:00 2026-05-17T17:52:05+00:00

I have DataTable which feeds a dropdown list. The values of this dropdown persist

  • 0

I have DataTable which feeds a dropdown list. The values of this dropdown persist when I click on the button whereas the datatable which feeds the list is null at next page reload. I need that this dataTable to also persist like its associated control so isn’t there anything that asp.net has in its tricks bag to behave like for controls but for private members instead ?

public partial class _Default : System.Web.UI.Page
{
    private DataTable m_DataTable = new DataTable();

    private void f(String x, String y){
        // some function of x,y
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            m_DataTable.Columns.Add("Title");
            m_DataTable.Columns.Add("x");
            m_DataTable.Columns.Add("y");

            m_DataTable.Rows.Add("place1", "1", "2");
            m_DataTable.Rows.Add("place2", "3", "4");


            int nTotalRecords = m_DataTable.Rows.Count;
            for (int i = 0; i < nTotalRecords; i++)
            {
                DropDownList1.Items.Add(m_DataTable.Rows[i]["Title"].ToString());
            }


            DataRow[] dataRowArray = m_DataTable.Select("[Title]='place1'");
            String x = dataRowArray[0]["x"].ToString();
            String y = dataRowArray[0]["y"].ToString();

            f(x, y);

        }


    }


    protected void ButtonGo_Click(object sender, EventArgs e)
    {
        // this code will crash because m_DataTable is emptied when page reloads
        DataRow[] dataRowArray = m_DataTable.Select("[Title]='place2'");
        String x = dataRowArray[0]["x"].ToString();
        String y = dataRowArray[0]["y"].ToString();
        f(x, y);
    }
}

Update: I heard that asp.net paradigm was to be as close as possible as desktop. Why does it create a new instance for the page instead of keeping it ? Why does it keep the same state for controls and not for private members that is incoherent isn’t it?

  • 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-17T17:52:06+00:00Added an answer on May 17, 2026 at 5:52 pm

    You can put it in view state:

    Page.ViewState["DataTable"] = m_DataTable;
    

    and retrieve it after postback:

    m_DataTable = Page.ViewState["DataTable"];
    

    The drawback is of course that the serialised data is sent to the client and back again, which increases the weight of the postback.

    Another alternative is to store it in the user session:

    Session("DataTable") = m_DataTable;
    

    and retrieve it after postback:

    m_DataTable = Session("DataTable");
    Session.Remove("DataTable");
    

    The drawback of that is of course that you increase the memory load on the server.

    Consider the drawbacks of each method, and weight that against how much it costs to get the data again. Sometimes time is the cheapest resource.

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

Sidebar

Related Questions

I have a DataTable which contains data in this format. I want to convert
I have a DataTable which I want to check if values in three of
This is the scenario: I have a web page with a ListView lv_create_claim which
----> I have datatable which is passing to another page in session variable. ---->
I have a datatable which loops through a List and has a value column
I have a DataTable which is bound to a GridView. I also have a
I have a DataTable which I want to save to a SQLite Database Table.
I have a DataTable which I select from database (Well, these data cross several
I have a DataTable which is bound to datagridview (Winforms)... I use the following
In C#/ASP.NET 3.5, I have a DataTable which pulls rows from the database. I

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.