RadAjaxManager ViewState and PreRenderComplete

2 posts, 0 answers
  1. Emmsa
    Emmsa avatar
    56 posts
    Member since:
    Oct 2006

    Posted 10 Feb 2012 Link to this post

    Hi there,

    I have a class called BasePage, it is a Page that helps me with common tasks. One is to persist some info in ViewState.
    This BasePage saves the ViewState info in the PreRenderComplete event (so the real page can modify this info in the PreRender event)

    The problem I have is that it does not work if I use RadAjaxManager

    Is there any way to solve this problem? 

    This is an example

    <form id="form1" runat="server">
      <div>
        <asp:ScriptManager runat="server" ID="scriptManager"></asp:ScriptManager>
        <telerik:RadAjaxManager runat="server" ID="manager">
          <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="lnk1">
              <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="lbl" />
              </UpdatedControls>
            </telerik:AjaxSetting>
          </AjaxSettings>
        </telerik:RadAjaxManager>


        
        <asp:LinkButton runat="server" Text="Sum with Ajax" ID="lnk1" OnClick="lnk_Click">
          </asp:LinkButton>


        <asp:LinkButton runat="server" Text="Sum without Ajax" ID="LinkButton1" OnClick="lnk2_Click">
          </asp:LinkButton>


        <asp:Panel runat="server" ID="pnlAjax">
          <asp:Label runat="server" Text="Label" ID="lbl"></asp:Label><br />
          <asp:Label runat="server" Text="Label" ID="Label1"></asp:Label><br />
          
        </asp:Panel>
      </div>
      </form>



    public partial class Demos_KeepInViewStateWithAjaxTest : Page
    {
      int _count;
      public int Count
      {
        get
        {
          return _count;
          
        }
        set
        {
          _count = value;
          
        }
      }
      protected void Page_Load(object sender, EventArgs e)
      {
        //Restore ViewState
        if (ViewState["Count"] == null)
        {
          ViewState["Count"] = 0;
        }


        Count = Convert.ToInt32(ViewState["Count"]);


        PreRenderComplete += new EventHandler(Demos_KeepInViewStateWithAjaxTest_PreRenderComplete);
      }


      void Demos_KeepInViewStateWithAjaxTest_PreRenderComplete(object sender, EventArgs e)
      {
        ViewState["Count"] = Count.ToString();
      }




      protected void lnk_Click(object sender, EventArgs e)
      {
        lbl.Text = Count.ToString();
        Count += 1;
      }


      protected void lnk2_Click(object sender, EventArgs e)
      {
        lbl.Text = Count.ToString();
        Count += 1;
      }
    }

    Darío K
  2. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 15 Feb 2012 Link to this post

    Hello Darío ,

    The described issue is actually expected in case Ajax is used on the page. You could test the same application with regular asp UpdatePanel instead of RadAjax and the behaviour will be the same.
    You could review the following online resources which elaborates on this matter:
    http://stackoverflow.com/questions/354195/inconsistent-behavior-with-ajax-and-viewstate-in-net
    http://encosia.com/why-aspnet-ajax-updatepanels-are-dangerous/
    http://www.webmasterworld.com/javascript/3600457.htm

    Regards,
    Maria Ilieva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top