Invalid viewstate information may be corrupted - asp.net

I keep getting the following error message when I run my vb.net web app:
The state information is invalid for this page and might be corrupted.
View full message here.
After a good search, I came across this Microsoft page, which describes the problem exactly. The likely cause seems to be "scenario 2":
Scenario 2: You modify your pages, which causes the shadow, copied files in the
Temporary ASP.NET files folder to be regenerated. A user has a copy of
the page that was requested before this change, and the user posts the
page after the files in that folder were regenerated.
But strangely- despite saying there is a hotfix, does not actually give the link to it.
Can anyone suggest a fix?
UPDATED: I appear to have prevented this error from happening by using EnableEventValidation="False" in the Page node of the markup as recommended here. More of a work-around than a fix.

It is not recommended to disable EnableEventValidation as explained in Page.EnableEventValidation Property.
I got this issue before and came over it by deleting all files within ASP.NET Temporary Folder.
Folder Path:
.NET 2 : C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
.NET 4: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

IN one of my project, I was getting this error in Mozilla Firefox only that whenever i clicked a button or link.
It is because of the fact that firefox caches the form fields. Two ways to encounter this problem.
Write following snipet in your cs file
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.Browser.MSDomVersion.Major == 0) // If it is Non IE Browser
{
Response.Cache.SetNoStore();
}
}
In page load, write following statemet
Response.Cache.SetNoStore();
Now error has been removed and you can sleep with satisfaction.
http://yourtahir.wordpress.com/2008/06/26/the-state-information-is-invalid-for-this-page-and-might-be-corrupted/

Might be overdate, but this cured my viewstate problems with ajax calls to page control.
Custom CompressedViewState :
add this code :
private ObjectStateFormatter _formatter = new ObjectStateFormatter();
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
MemoryStream ms = new MemoryStream();
_formatter.Serialize(ms, viewState);
byte[] viewStateArray = ms.ToArray();
ClientScript.RegisterHiddenField("__COMPRESSEDVIEWSTATE",
Convert.ToBase64String(
CompressViewState.Compress(viewStateArray)));
}
protected override object LoadPageStateFromPersistenceMedium()
{
string vsString = Request.Form["__COMPRESSEDVIEWSTATE"];
byte[] bytes = Convert.FromBase64String(vsString);
bytes = CompressViewState.Decompress(bytes);
return _formatter.Deserialize(
Convert.ToBase64String(bytes));
}

Related

Validation of ViewState MAC failed and Page.MaintainScrollPositionOnPostback

I know there are at least 20 questions related to "Validation of ViewState MAC failed", but mine is a bit unique (at least i think it is)
My website was working fine before I had introduced this code on code-behind of my master page.
protected override void OnInit(EventArgs e)
{
Page.MaintainScrollPositionOnPostBack = true;
base.OnInit(e);
}
This obviously brings a section of a page into view after postback. After adding this code my website starting giving me "Validation of ViewState MAC failed" exception on click of a button which calls the following JS code.
function SelectorSubmitOld(targetUrl) {
var f = jQuery('form').get(0);
jQuery("#__VIEWSTATE").remove();
f.action = targetUrl;
f.submit();
}
As soon as I comment the code written in code-behind of MasterPage, everything comes back to normal.
The following I tried other than the above mentioned (which was obviously not required)
My application is hosted on just 1 server
Machinekey is present in web.config and removing/changing it did not solve my problem
Setting EnableViewStateMAC=false in web.config or at page level did not solve my problem
My question is, I am unable to understand what is the relation between my code and ViewStateMAC?
Please let me know if you need any further information from my side.
Thanks in advance!!
ASP.NET Web Forms (.aspx) pages are meant only to post back to themselves, not to any other page. In your case, you have A.aspx posting to B.aspx by changing the <form action> parameter at submission time.
What's the scenario you're trying to accomplish by changing the POST URL dynamically? Perhaps we can suggest a better way to do this.
From this post, I would try to remove references to postback posiiton with :
function SelectorSubmitOld(targetUrl) {
var f = jQuery('form').get(0);
jQuery("#__VIEWSTATE").remove();
jQuery("#__SCROLLPOSITIONX").remove();
jQuery("#__SCROLLPOSITIONY").remove();
f.action = targetUrl;
f.submit();
}

Uploading large file just returns blank page in asp.net

Suddenly, my ASP.NET web application is returning completely blank HTML pages after you upload overly large files. (Like a 6MB file - I've set the various request lengths and file upload settings to limit it to 5MB).
The page is really blank, View Source in chrome reveals nothing, completely empty. In IE, it's a "This page can’t be displayed" error.
Stepping through the code, it's clear that the codebehind of the upload buttons (which executes when the file is small) is never executed. The blank page comes up instantly as soon as the file is uploaded.
I put a pretty standard Application_Error() method in Global.asax and it catches the error you'd expect (HttpUnhandledException.ErrorCode = -2147467259) and it can Server.Transfer() the user to my own custom error aspx page, I can even hit a breakpoint in the page load of that and step through it, but once it's done... I just get the blank page.
Anyone have any ideas? Something I could try?
ASP.NET terminates processing the request if its length exceed allowed limit set in maxrequestlength.
If you want to catch the exception, try to get it as explained at
How to catch ConfigurationErrorsException for violating maxRequestLength?
Another way is to set maxrequestlength higher than the required limit and check file size in the code
protected void button_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile && fileUpload.FileContent.Length > 5*1024)
{
// too big
}
}

Dynamic Gallery is not working online but working on localhost

i have dynamic web Gallery in light box which is working fine in localhost but when i publish a site and then upload on server, gallery is not showing the images online...
i had to take the page property "AutoEventwireup=true" because i need to call page events automatically... and i think it`s creating problem online but working fine in localhost..
is my assumption correct about AutoEventWireup="true"???
Note: i have already granted a permission to image folder in server so it`s not a cause of configuring the IIS...
Any Help or suggestion plz?
Updated:
Download the Demo of photo Gallery (no database required it will be handled by xml data)
You can upload from admin and display to client, there are two pages first is imageupload.aspx in Admin folder which will be uploaded and second is photogallery.aspx where images will be displayed and the third one which i have to mention is xml file in which xml nodes are created, xml file is inside a data folder
Download Link
https://drive.google.com/file/d/0B_f0_xt7mMiwTXc2T0dFS0MzWE0/edit?usp=sharing
No your assumption is not correct. AutoEventWireup="true" allows you to handle standard page events using their default names, for example: -
protected void Page_Load(object sender, EventArgs e)
{
// your code
}
If you set AutoEventWireup to false, you must explicitly override the named function: -
protected override void OnLoad(EventArgs e)
{
// your code
}
Can you post your code please - the page, and the image gallery ?

Custom control does not exist exception in SharePoint

I'm trying to add a custom web UserControl to my SharePoint application. I have added my UserControl to a SharePoint WebPart and now i'm trying to load that WebPart in my page.
But when i add the WebPart i get the following exception:
The file /usercontrols/Test3Report.ascx does not exist.
But i'm 100% sure that both the file and folder exists. The path to the usercontrol also seems fine to me. This is how i try to load my control:
protected string UserControlPath = #"~/usercontrols/";
protected string UserControlName = #"Test3Report.ascx";
private Test3Report mycontrol;
protected override void CreateChildControls()
{
try
{
mycontrol = (Test3Report)this.Page.LoadControl(UserControlPath + UserControlName);
Controls.Add(mycontrol);
}
catch (Exception CreateChildControls_Exception)
{
exceptions += "CreateChildControls_Exception: " + CreateChildControls_Exception.Message;
}
finally
{
base.CreateChildControls();
}
}
This seems right to me. I have no idea why it is complaining that i cannot find the Test3Report.ascx.
I've read something about the TrustLevel not set to full or something. Could that be it? Or any other ideas?
Try adding your new custom control by right-clicking on your solution in solution explorer in VS2010 -> add -> new item , after choosing sharepoint2010 from the list on the right, choose "User control".
This way your control will be automatically created and later deployed in the mapped "~/_ControlTemplates/PROJECTNAME/UserControlNAME.ascx" folder which can be referenced to later.
Also make sure that the user control is included in the package by opening the ".package" file in "Package" folder under solution explorer and checking that the user control is listed under "Items in Package".
Tip: If possible for your use-case, instead of adding the custom control programmatically in CreateChildControls() you can put your destination page in design mode and then add your custom control by dragging and dropping the .ascx file from the solution explorer into you page.

ASP.NET Save HTML Sent to Browser

I need to save the full and exact HTML sent to the browser for some transactions (for legal tracking purposes.) I suspect I am not sure if there is a suitable hook to do this. Does anyone know? (BTW, I am aware of the need to also save associated pages like style sheets and images.)
You can create an http module and have the output stream saved somewhere.
You should hook to PreSendRequestContent event...:
This event is raised just before ASP.NET sends the response contents to the client. This event allows us to change the contents before it gets delivered to the client. We can use this event to add the contents, which are common in all pages, to the page output. For example, a common menu, header or footer.
You could attach to the PreSendRequestContent. This event is raised right before the content is sent and gives you a chance to modify it, or in your case, save it.
P&P article on interception pattern
You could implement a response filter. Here is a nice sample that processes the HTML produced by ASP.NET. In addition to the HTML being sent to the client you should be able to also write the HTML to a database or other suitable storage.
Here is an alternate and IMO much easier way to hook the filter into your application:
in Global.asax, place the following code in the Application_BeginRequest handler:
void Application_BeginRequest(object sender, EventArgs e)
{
Response.Filter = new HtmlSavingFilter(Response.Filter);
}
I suppose you only want to save the rendered html for certain pages. If so, I have been using the following approach in one of my applications that stores the rendered html for caching purpose somewhere on the disk. This method simply overrides the render event of the page.
protected override void Render(HtmlTextWriter writer)
{
using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
{
base.Render(htmlwriter);
string html = htmlwriter.InnerWriter.ToString();
using (FileStream outputStream = new FileStream(#"C:\\temp.html", FileMode.OpenOrCreate))
{
outputStream.Write(html, 0, html.Length);
outputStream.Close();
}
writer.Write(html);
}
}
Really works well for me.
There are also hardware devices made specifically for this purpose. We've used one called "PageVault".

Resources