Solved

Store the Data Table in Viewstate

Posted on 2009-05-05
2
607 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
2 Comments
 
LVL 5

Accepted Solution

by:
Krummelz earned 500 total points
Comment Utility
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
Comment Utility
Excellent. I did my own research and came up with the same answer.

thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now