invalid viewstate with AJAX widget
I have a custom widget that makes AJAX requests that query content items in Sitefinity. The requests are set to run on a timer. The widget is working. I place this widget on a page. It will run indefinitely without any problems. When I go into Sitefinity to edit a page that this widget is on it will continue to work until I hit publish. Once I hit publish I get errors.
I get a 400 bad request error but no .NET exception page. The error log is what pointed me in the direction of an invalid viewstate error. I went ahead and added a predefined machinekey to my web.config but that didn't fix my issue.
Any ideas?
Hi Jaime Mccracken,
The behavior you describe is indeed very strange - the fact that it works until you republish the page, I mean. Have you checked the Enable ViewSate property of the page where your control is dropped, and would it be possible for you to test the same functionality using this demo for the Ajax timer and let us know if the issues persist. Thank you in advance for your cooperation.
All the best,
Boyan Barnev
the Telerik team
I used the demo you linked above and created a simplified user control that I dropped on a page as a custom widget.
All the ajax timer does is change the text on a label every 3 seconds.
sampleAjax.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="sampleAjax.ascx.cs" Inherits="Telerik.AJAXExamplesCSharp.AJAX.Examples.Common.AjaxifyTimer.DefaultCS" %>
<
div
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
>
</
telerik:RadScriptManager
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"Timer1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label1"
LoadingPanelID
=
"LoadingPanel1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
telerik:RadAjaxLoadingPanel
ID
=
"LoadingPanel1"
runat
=
"server"
/>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"Label"
></
asp:Label
>
<
asp:Panel
ID
=
"Panel1"
runat
=
"server"
>
<
asp:Timer
ID
=
"Timer1"
runat
=
"server"
Interval
=
"3000"
OnTick
=
"Timer1_Tick"
/>
</
asp:Panel
>
</
div
>
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Threading;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
Telerik.Web.UI;
namespace
Telerik.AJAXExamplesCSharp.AJAX.Examples.Common.AjaxifyTimer
/// <summary>
/// Summary description for DefaultCS.
/// </summary>
public
partial
class
DefaultCS : System.Web.UI.UserControl
public
void
Page_Load(
object
sender, System.EventArgs e)
Label1.Text =
"Page Load"
;
public
void
Timer1_Tick(
object
sender, EventArgs e)
Label1.Text = DateTime.Now.ToString();
Hello Jaime Mccracken,
Thank you for getting back to me. I have tested the control on several local projects, but the ViewStateissue does not reproduce on our side. We have tested with both ViewStateturned on and off, and in both scenarios the control was working fine when editing/republishing the page. My suspicion was that the problems might arise fromt he fact that once you have published the page, the ViewState would not be valid since the page has been changed, however it looks like this is not the current issue. Please take a look at the sample video I've recorded for you while testing this functionality, and do not hesitate to let me know if there's anything I might be missing. Looking forwards to your reply.
Best wishes,
Boyan Barnev
the Telerik team
I believe I got to the bottom of this issue.
I started with a blank project and I got the experience that you shared with me. The ajax timer is broken after the page is published in the background but the page doesn't throw an exception. The label just keeps blinking. I believe that is to be expected since the page has changed.
My issue is happening because I have CustomErrors turned on in my web.config. If you turn on CustomErrors and have a default error page, all of a sudden the page is getting redirected to an error page.
Is there any way to catch the invalid viewstate exception? If I turn off the CustomErrors the broken ajax is a much better experience than being redirected to an error page. I would l like to handle that exception myself but I'm not sure how to catch it. Any ideas?
Hello Jaime Mccracken,
That's very interesting - if you inspect the video closer, you'll notice that the control is actually working fine on my local project. I have just tested it on several other projects, and the behavior was consistent - even after republishing the page, the control was updating properly updating itself, with no viewstate exceptions being thrown. However, if any problems persist, I'd recommend you to take a look at some lightweight javascript that performs similar functionality for example this one
All the best,
Boyan Barnev
the Telerik team
I'm not sure if you saw this but I said the issue is caused by having CustomErrors turned on in the web.config. Did you try that? If you don't have CustomErrors turned on the AJAX stops working but doesn't cause any errors or exceptions on the page.
Hello Jaime Mccracken,
I've had customErrors element set to Off on both tests, actually what's been really peculiar is that Ajax seemed to work on my side, even after publishing the page, you can spot on the sample video that the timer on the live page is still working. If the problems persist, please let us know of your exact setup so we can try replicating the issue on our side. Thanks in advance for the kind understanding.
Greetings,
Boyan Barnev
the Telerik team
I noticed on your sample video at 2:28 you published your page and the ajax looks like it is updating but look at the time. It isn't updating. Did you notice that?
Hi Jaime Mccracken,
Thank you for pointing this out, indeed you are absolutely right - the control stops ticking once you publish the page. I have verified with our developers team that this is an expected behavior, since the page is being recompiled on Publish, so the page that's being observed on the frontend needs to be reloaded in order for the viewstate to be adjusted (since the version of the page on the frontend is different that the actual compiled version after publishing the page). In general their recommendation is to stick to javascript-based implementation for this task as it's much easier to achieve this using client-side code, and there won't be any unnecessary server calls.
All the best,
Boyan Barnev
the Telerik team