Solved

Store the Data Table in Viewstate

Posted on 2009-05-05
2
611 Views
Last Modified: 2012-06-27
I ran accross this code in a Project :

                 private string vsCounselors = "dtCounselors";

                   public void ReLoad(bool refresh)
        {
            // Define our variables.
            DataTable _dt = new DataTable();

            try
            {
                // Get the datatable containing counselors information.
                if (refresh)
                {
                    _dt = CodeLayers.BL.AdminCounselorBL.GetCounselors();
                    ViewState[vsCounselors] = _dt;
                }
                else
                {
                    _dt = ViewState[vsCounselors] as DataTable;
                }

                if (_dt.Rows.Count > 0)
                {
                    dlCounselors.DataSource = _dt;
                    dlCounselors.DataBind();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
What is ViewState[vsCounselors] variable?

dtCounselors is not a control on the page.


thanks
0
Comment
Question by:Faye_DBA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
Krummelz earned 500 total points
ID: 24314064
Viewstate is a type of storage space (an array) in .NET that is used in the background by .NET, to store data for controls accross postbacks.

So for example, if you have a textbox control and a drop down list which is set to cause an auto postback; and you just typed something into the textbox, and then selected an item on the drop down list. The drop down list will cause a post back, and when it does that, you textbox will not lose the data that you typed into it - because its saved in Viewstate automatically.

BUT, you are also able to use Viewstate as you wish to keep data across a postback. It works in much the same way as Session, except that each page has its own Viewstate - so one page's viewstate wont be the same as the next page's viewstate. The name "vsCounselors" in braces after the word Viewstate, is the name it uses to store your data under. You also use that name to retrieve the data you stored there.

So in your code example, a Datatable is being stored in a Viewstate variable called "vsCounselors" if the refresh variable is true. If refresh is false, it gets the value inside the "vsCounselors" Viewstate variable and stores it in a Datatable called "_dt".

If the Datatable "_dt" has rows, then it gets used as the datasource for "dlCounselors".
0
 

Author Closing Comment

by:Faye_DBA
ID: 31578153
Excellent. I did my own research and came up with the same answer.

thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 93
How to filter by key press ? 6 66
Calculating percentage 2 47
e commerce steps shown instead of self testing 2 56
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question