Click here to Skip to main content
15,886,788 members
Everything / ViewState

ViewState

ViewState

Great Reads

by ASP.NET Community
ViewState is the mechanism that allows state values to be preserved across page postbacks.Because of the stateless nature of web pages, regular
by Mike Thom
If you move your ViewState from the top of the page to the bottom, you will get better search engine spidering.Step 1Create a class file in App_code folder of your application and name it as PageBase.cs. Copy the following code to the class file.using System.IO;using...
by yuvalsol
Keep the view state out of the web page and store it in SQL Server
by Xnko
On the Web there are many examples for moving ViewState to the bottom of Page using RegularExpressions. This is another way using HtmlTextWriter.All you nees that add in your project class named MoveViewStateHtmlTextWriter and write in your BasePage code below.protected override...

Latest Articles

by yuvalsol
Keep the view state out of the web page and store it in SQL Server
by Mathi Mani
In this tip, we will try to understand whether viewstate and postback data are the same or different.
by Eduardo Antonio Cecilio Fernandes
This article explains how to manage ASP.NET Client and Server State, via View State, Hidden Fields, Cookies, Query Strings, Session State and Application State.
by ASP.NET Community
ViewState is the mechanism that allows state values to be preserved across page postbacks.Because of the stateless nature of web pages, regular

All Articles

Sort by Score

ViewState 

28 Feb 2014 by ASP.NET Community
ViewState is the mechanism that allows state values to be preserved across page postbacks.Because of the stateless nature of web pages, regular
28 Apr 2011 by Albin Abel
If you know about Personalization you can very well to store and retrieve user specific informations instead of session. However those objects needs to be serializable. This is an alternate suggestion. If you already know about personalization in .Net i don't need to write more on it :)
25 Mar 2011 by OriginalGriff
Start at the beginning: MSDN[^]If this doesn't provide enough, then Google will help with more advanced / specific subjects.
28 Apr 2011 by wooga111
Hello All,Here is my scenario:I am using VS2010 and have a ASP.NET web application. On my page I am using a WebUserControl that uses a modelpopupextender to allow a user to select contacts from a list. All of this is contained in a asp.net formview.After a user selects some contacts...
25 Jul 2017 by Sheila Pontes
Hi, To you insert the records into the database, you need to read each row in the gridview by inserting the records. Here! An example. private void Booking_Tax(Int32 id) { string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; ...
14 Apr 2011 by Mike Thom
If you move your ViewState from the top of the page to the bottom, you will get better search engine spidering.Step 1Create a class file in App_code folder of your application and name it as PageBase.cs. Copy the following code to the class file.using System.IO;using...
14 Sep 2013 by Rohit kumar6210
Any one Please help me to solve the following error ? Please tell me why the following error occurs ????Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation...
17 Sep 2013 by Harshil_Raval
Hi,Try this.protected void addemp_Click(object sender, EventArgs e){ DataTable empemails = new DataTable(); int srno = 1; empemails.Columns.Add("Sr.No"); empemails.Columns.Add("EmpName"); empemails.Columns.Add("Email"); foreach...
17 Sep 2014 by Richard Deeming
You'll get better results if you bind the list in the Init event of the control, rather than the page.protected void ddlCustomers_Init(object sender, EventArgs e){ var ddl = (DropDownList)sender; ddl.DataSource = SystemData.GetAllCustomers(); ddl.DataTextField =...
24 Jun 2010 by rudolphgatt
Hi, I'm adding a DataSet object to the viewstate in the Page_Load().protected void Page_Load(object sender, EventArgs e){ string path = "..."; DataSet ds = XmlOperations.GetXmlFile(path); ViewState["DataSet"] = ds; ...}Then at some point I need to change the...
24 Jun 2010 by Sandeep Mewara
rudolphgatt wrote: DataSet object to the viewstate Not a good practise as DataSet's can be huge and storing them in ViewState might affect performance. rudolphgatt wrote:Can you change a ViewState object on postbackYesrudolphgatt wrote: How?What you have done in btnSave looks fine....
28 Jul 2010 by EdwardHitler
HelloIs it possible to remove the Row Select colour from a Gridview after another search has been performed from a search box?Thank you.Peter
2 Sep 2010 by Dalek Dave
Yes.Have a default colour and reapply it after any selection.
2 Sep 2010 by Steve Lydford
Have you tried something like this in the Search Button click event?GridView1.SelectedRow.RowState = DataControlRowState.Normal;
25 Mar 2011 by pavankumar from hyderabad
Thanks in advance please help me out.
13 May 2011 by praveen_gpk
I have a textbox..i want to use this textbox as autosearch of one column names eg:firstname..and now i have a table in viewstate ..so my requirement is to search firstname from viewstate table..How can i do that?only textbox value with database is i can get but i want to search the particular...
13 May 2011 by Ramalinga Koushik
Hope this[^] might help you.
15 May 2011 by Hemant__Sharma
If i'm getting your question right you have a datatable in the viewstate and you want to add all the values from rows of a particular column to the list you have shown above. if that is the case you can:1- Get Datatable from viewstate. DataTable dataTable = ViewState["myDataTable"]2- Get...
19 Apr 2012 by Bijesh Nair
Hello EveryOne..I have hosted my ASP.NET web application on server and its working fine. but sometimes when i try to browse pages, i get follwing errorValidation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration...
19 Apr 2012 by Mohamed Mitwalli
Hi , Check this one Here[^] Best RegardsM.Mitwalli
22 Apr 2012 by Ashish Sehajpal
Put the same machinekey on all the servers (usually it happens when application is hosted on multiple servers in a cluster or webfarm)P.S. - the machinekey will go inside System.web section in web.config.
10 Oct 2012 by itskvini
Hi,I have two drop down list. On each of these drop down's selection some code is there.But page is taking too much time to call these events.I have checked this within updatepanel and without updatepanel, but the result is same. How can I minimize the loading time.Not : - 1st drop down...
24 Jan 2013 by gouravkaila
Thanks All,Its working fine now.default__doPostBack = __doPostBack; var theForm = window.parent.self.firstGrid.document.Form1;
24 Feb 2013 by Shailesh vora
Thanks for posting answer.yes I have done like that but still sometime issue was coming so instead of .ascx control i have created my custom control so now waiting lets see :) issue still persist or not.
27 Mar 2013 by Member 4643583
Hello, I am using url rewriting with asp.net project having Ajax on page. but on post back when I am checking value of ScriptManager1.IsInAsyncPostBack and Page.IsPostBack its always false. After detail analysis I found that all values stored in ViewState lost during postback.Code in...
13 Jul 2013 by Sugu Thomas
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.Security;using System.IO;using System.Data;using System.Data.SqlClient;using System.Web.Configuration;using...
13 Jul 2013 by Zafar A khan
Create both Category and subcategory in one button click (i-e) create both in single click
14 Sep 2013 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Please refer the accepted answer - Validation of viewstate MAC failed[^].
21 Sep 2013 by Abhishek Luv
Issue : When the user is writing some text in the textarea(UserControl2) and in between when he tries to click anyone of the Radiobutton(from UserControl1) the page is posted back and the text value of the textarea is lost. I have also enable viewstate for the textarea.Please guide me. How...
22 Sep 2013 by Madhu Nair
Text Area is getting clear because the page is getting posted back on click of Radio Button. To over come the issue try saving the Text Area value in ViewState and re-populate it again from the view state
8 Nov 2013 by chitransh635
I have been working on ajax control (Rating) in this when i am filling 3 star value is stored in viewstate and then on as same page i am using page1 and page2 value is moving from 1 st page to another page bt when i am clickning on previous button then answer sshow me 3 but rating star is not...
18 Dec 2013 by Kasper Simonsen
HiI'm stuck at retrieving data from my Dynamically added Text boxes in ASP.NET.I Master Site and a Content site. I have added some buttons to the content site there are addíng or removing the textboxes, after what's needed by the user.My problem is, that i'm not sure how to retrieve...
18 Dec 2013 by prabhugi2it
HI,Please check the below sample.]]> Untitled Page ...
13 Jan 2014 by Member 10522730
I have two text box one with the view state property false and one is true and there is a button. When i run the web appln and fill some value in text box and click on button it is retrieveing the value of both the textbox whereas it shouldnt as one of the text has property false? please explain...
14 Jan 2014 by JoCodes
This behaviour is for the Web Controls which internally implements IPostBackDataHandler interface. To understand more on it. Refer View State for TextBox and other controls that implement IPostBackDataHandler[^]Hope this helps...
24 Jan 2014 by rahulDer
Hello there,m getting large amount of data in URL sizw of approximately 9KB and it causes an error like "Bad Request - Request Too Long"and while debugging ,m being sent to this error message of AjaxController...
24 Jan 2014 by Ahmed Bensaid
Hello,Maybe there are too many cookies on your website ;)http://stackoverflow.com/questions/10977655/http-400-request-header-too-long-error-when-accessing-asp-net-application[^]
30 Jan 2014 by Member 10560425
The following is my code to generate multiple textboxes and drop down lists based on the value of the variable nov(also entered by user in a textbox).i want to add the values of the texbox to a string (convert the values to string and generate a sql create table query).on the button click i am...
30 Jan 2014 by Shameel
The simplest way is to have a hidden control in your page and set its value to the concatenated values of all the dynamic controls before postback using JavaScript. On the server side, extract the value of the hidden control and use it as necessary.
16 Apr 2014 by Member 10755252
Hi. I need help with figuring out how to get my dynamically created Telerik dock controls to visually persist and recreate given the following scenario.I have a dashboard page that is displayed withing a master page container (don't know if this details matter, just outlining this).In it...
28 May 2014 by Shadow codder
Hi ,I am trying to populate the dropdownlist in gridview edit mode instead of label but I keep getting this error , as I found out its because I am putting DDL instead of label but everyone did the same thing and no problem at all for them , please help me to find why I keep getting this error...
28 May 2014 by Shadow codder
change the DDL line to : But still have problem It wont populate
19 Aug 2014 by Nkhanedzeni
Hi Everyone,Please help me. I am moving from Web Forms to Windows Forms and i have being using Viewstate and LINQ to SQL to connect to the database.I tried to use viewstate on windows forms and it throws errors. i have no idea on how can i change my class so that i works fine with windows...
19 Aug 2014 by Stephen Hewison
Win Forms programming uses a completely different approach to Web Forms.With web forms the client code (HTML/Browser page) is detached from the Server Side (Application code) so ASP.Net uses the View State and Session State to manage application state. This problem is made worse by...
5 Sep 2014 by thatraja
This could help you on few things.Improve Everything[^]
17 Sep 2014 by Abdallah Al-Dalleh
Hello all,We all know the problem of very huge view state when binding the dropdownlist for example. Few weeks ago I searched for it and the solution I found was to override OnInit method. Take this for example:protected override void OnInit(EventArgs e){ ddlCustomers.DataSource =...
17 Nov 2014 by fkjasakdjklasj
Hi all, I have a problem. I want replicate an effect "infity scroll". So I have an aspx page with inside an user control that load some row. When capture scroll event I want to load another user control with some other rows. This is correct for me, but when after load some other post I try...
27 Mar 2015 by AnoopkumarS
How can i set ViewState["Drivers"] to null using jQuery in ASP.NET ?ViewState["Drivers"]= null;
27 Mar 2015 by Afzaal Ahmad Zeeshan
You should know that ViewState (and other MVC's objects) are server-side and are manipulated on the run-time, after the content is sent (in HTML form) to the client, there is no connection to those objects anymore. To manipulate the DOM then, you have to use JavaScript (or jQuery specifically)...
20 Apr 2015 by Abhinav S
Guide on How to preserve state[^] provided here.Another link - Managing Tombstone State in a Windows Phone 7 Application[^].
12 May 2015 by Kats2512
Hi,I have an issue on a menu page that has a list of links.I have added a new asp panel to a different page but on my links page it comes up with an error saying:Failed to load viewstate. the control tree into which the viewstate is being loaded must match the control tree that was...
12 May 2015 by Andy Lanng
Ok. The issue is that some controls are finding there way into the control tree that are not expected by the postback event. The page will error when the control tree is not as expected for a couple of reasons:1: Basic security. It prevents people 'faking' postbacks to fish for data from...
3 Feb 2016 by jitendra raj
Dear friend I am jitendra kumar working on an Web based ERP Application I want to store multiple values of an column in viewstate and retrive one by one in string type variable to send from one page to another page.using System; using System.Collections; using...
1 Sep 2016 by Member 11711524
I had the same issue. This issue was at client end but it didn't occur in my local system.After hours of googling, i had written EnableViewState="false" to my table tag in aspx page which has all the dynamic controls and then i removed all the viewstate variables and instead i created some...
28 Oct 2016 by Member 12821924
Hello Im having an issue saving my dataTable to a ViewState. GetDataTable takes a SQL command and retrieve data from database. The problem I am facing is that the ViewState is NOT storing the datatable in the code below, however the dataTable has data stored within. I am using VB to write this...
28 Oct 2016 by Vincent Maverick Durano
I'm not sure if the issue is about the case sensitivity of your ViewState keys. You set the value to ViewState("orignDt") and you get the value using ViewState("originDT") - noticed the capital T.Try to fix that so it will match the case. Also try to debug your codes, step into it and see if...
19 Nov 2016 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
You have to store the checkbox indexes or something to recognize them somewhere may be in a Session or ViewState property. While loading the grid, inside RowDatabound, you can check that checkbox if it is present in the session, simple.
20 Nov 2016 by OriginalGriff
Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.Posting this here relies on them...
20 Nov 2016 by Vincent Maverick Durano
I agree with the member above, that's the nature of the web. Web Apps are stateless and you have to do something to persist the state of the CheckBox that resides in a normal grid or paged grid. You'll have to track the selected page and the CheckBox that was selected and store it somewhere like...
21 Nov 2016 by Member 12859772
I solved this problem. if (!Page.IsPostBack) { //Uruns(); //Projes(); //Siparislers(); Elemans(); Musteris(); Kategoris(); } else ...
13 Mar 2017 by yuvalsol
Keep the view state out of the web page and store it in SQL Server
8 Dec 2018 by AnkushK1
Have you tried using a HiddenField in your page? You can save the Guid ID as a plain string into that.
11 Dec 2018 by AnkushK1
I don't think using querystring is a good idea. However, i tried this scenario and it worked for me. I added 2 buttons and labels. On button 1 click, i generated a guid and stored it in a session variable and set it as text of label 1 and on click of 2nd button, i get the guid value from session...
24 Jun 2021 by Member 15260864
I have a CrystalReportViewer, and when I am using it to report a report, it automatically stores the rpt path in View state. On decoding the view state, the path of the rpt is visible. How to stop this? On disabling View State for the...
24 Jun 2021 by Richard Deeming
Unless a control is specifically written to function properly with ViewState disabled, you cannot disable its ViewState and expect it to work. However, you can set the Page.ViewStateEncryptionMode property[^] to encrypt your ViewState field. ...
19 Apr 2010 by Xnko
On the Web there are many examples for moving ViewState to the bottom of Page using RegularExpressions. This is another way using HtmlTextWriter.All you nees that add in your project class named MoveViewStateHtmlTextWriter and write in your BasePage code below.protected override...
10 Jul 2014 by Eduardo Antonio Cecilio Fernandes
This article explains how to manage ASP.NET Client and Server State, via View State, Hidden Fields, Cookies, Query Strings, Session State and Application State.
3 Jan 2015 by Mathi Mani
In this tip, we will try to understand whether viewstate and postback data are the same or different.
6 Feb 2013 by Michiel du Toit
Hi,You have to (re)create your dynamic controls in the Page_Init event - which occurs before the ViewState is loaded. You can create controls for the first time in Page_Load or most events - just make sure to recreate them in Page_Init when the page gets loaded again.If you need to...
19 May 2011 by EnkelIk
Hi all,We just switched target framework in our ASP.NET web application from 3.5 to 4.0. We ran into the following problem:We have a couple of custom controls that worked fine in 3.5 but now with 4.0 they are not persisted in ViewState, one of them is basically a wrapper for other...
16 May 2011 by praveen_gpk
Suppose I have a data in a list like below and now I want to get data from viewstate which already contain table in that I want one column. How can I do that?public List GetEmployeeList() { List empList = new List(); empList.Add(new...
11 Jan 2013 by gouravkaila
Hello Mentors,Could anybody tell me how can i use __dopostback method to call the codebehind of another page.I want to override the __dopostback so that i could change the value of theform and set it equal to the form for which i want to call the _postback.this is what asp.net does...
1 Sep 2016 by Shailesh vora
Hi,I have created dynamic controls in asp.net web application.for that i followed below steps1) I have created my DropDown List custom(.ascx) control in which i have mentioned following properties to enahance it's existing functionality.MyDropDown.ascx
14 Mar 2013 by Cleops
Hi!I've got a custom web user control that consists of a panel and 12 imageMaps on it. Now I've added a member variable like this:ImageMap[] ProfileImages = null;And in the OnInit method, i do this:ProfileImages = new ImageMap[12]; ProfileImages[0] = ImageMap1; ProfileImages[1] =...
13 May 2014 by Member 8680235
I have very simple code in which I just want to check user have selected the record in grid or not for this what I have done is public void chkSelect_CheckedChanged(object sender, EventArgs e) { CheckBox cbChkAllocate = (CheckBox)sender; GridDataItem dataItem =...
20 Apr 2015 by Aditya_Goud
I am making a Windows Mobile Application, i have several pages in it, when i go from one page to other page the values in the previous page are getting erased.. in one of the pages i have a Inkpresenter to make a signature in my app. i need to save that .. and over all i also want to know how to...
25 Jul 2017 by sam_matte
my question depends upon my need to populate a table in database, from a gridview. I've populated that gridview from a datatable from ViewState. Can anybody help me? What I have tried: string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; ...
18 Aug 2014 by Member 8073143
Greetings to all of you!! Currently I am working on Payment gateway integration with my ASP.NET application, in which I have to post few form variables to Payment Gateway page using GET method. When I do it using simple HTML page using form controls to hold the values and post it to external...
20 Nov 2016 by Mohtshm Zubair
put all code of Page_load event in if (!Page.IsPostBack){ // all your code here}
12 Oct 2012 by TIKSN Int
Hi guys.I have a problem with a view state.The problem is when I am trying to get value it is giving me the default one but show me correct value from view state.Please Help.Here is the code.using System;using System.Collections.Generic;using System.Linq;using...
12 May 2014 by AR547
Solve this issue regarding dropdownlist and gridview. The scenario is as following: I have one dropdownlist and one gridview. Data from database is binding to dropdownlist. now dropdownlist has 1000+ employee names in DataTextField and employee emails in DataValueField. there is an ADD button...
28 May 2014 by Shadow codder
solved already just need to recall the gridview in edit even protected void smOrderShipmentList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { smOrderShipmentList.EditIndex =-1; LoadData(); }
17 Jun 2014 by bobb024
Hey Everyone,I have an application where I have been trying multiple different things because I deal with such slow connections. I have an ASP.NET 4.0 website with webforms and controls and classes, pretty standard. I have tried to fill up all the user data and store it in the session and...
3 Feb 2016 by Manas_Kumar
View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a...
21 Nov 2016 by Member 12859772
Hi I am using Product Table. I choise Category DropDownList after page is postback and I choise checkbox and I choise another DropdownList after page is postback and I lost value Checkbox Checked. How can I do example.What I have tried: ...
20 Nov 2016 by Member 12859772
private void Uruns(){ //DataTable dt = new DataTable(); //dt.Columns.Add("ID"); //dt.Columns.Add("UrunAd"); //dt.Columns.Add("Stok"); using (ApricumMedyaEntities medya = new ApricumMedyaEntities()) { int katID =...
20 Nov 2016 by Mohtshm Zubair
And you can also set checkbox control property autopostback to true from properties windows. First select the control on designer window and then press F4 or right click and select properties.
20 Nov 2016 by Mohtshm Zubair
forget what I tell you above now follow following flowWhat you need to do is - try to trigger postback event on every check or make an ajax call to save the id of that record which is checked in browser, in any temporary static list or session.- And after that whenever any post back...
20 Nov 2016 by Mohtshm Zubair
Listen when you are making selection in dropdown the post back is triggering in as the code you paste aboveprotected void drpKategori_SelectedIndexChanged(object sender, EventArgs e) { Uruns(); }This Uruns method is again binding the grid due to...
11 Dec 2018 by Member 8583441
I have created a asp.net web application for that i am having two asp.net buttons in same page. First button for inserting the data and the second button for updating the data in SQL Server. Insertion part is done and creating an id. This id is Uniqueidentifer. I want that id when second click...