This is a migrated thread and some comments may be shown as answers.

RadAjaxManager ViewState and PreRenderComplete

1 Answer 94 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Emmsa
Top achievements
Rank 2
Emmsa asked on 10 Feb 2012, 05:22 PM
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

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 15 Feb 2012, 01:33 PM
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 >>
Tags
Ajax
Asked by
Emmsa
Top achievements
Rank 2
Answers by
Maria Ilieva
Telerik team
Share this question
or