IT Community - Software Programming, Web Development and Technical Support

Ajax and Viewstate variables

This is a discussion on Ajax and Viewstate variables within the C# Programming forums, part of the Software Development category; Hello Everyone, I wanted to keep a counter for the page, which will increment or decrement based on button clicks. ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-20-2007, 07:59 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Ajax and Viewstate variables

Hello Everyone,


I wanted to keep a counter for the page, which will increment or decrement based on button clicks.

These buttons are kept in UpdatePanel so not page refresh happens.

I decided to use viewstate variable as counter, I am initializing that variable to 0 for the first call.

but I found that for every click I have to initialize the viewstate variable as it is not maintained across Ajax request calls,.

Is there any workaround for this?



Thanks in advance,

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-20-2007, 08:10 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: Ajax and Viewstate variables

Following works just fine for me...



<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />

<asp:UpdatePanel ID="UpdatePAnel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblClickCount" runat="server" Text="0" />

<asp:Button ID="Button1" Text="Add counter" runat="server" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>



Protected Sub Button1_Click(Object sender , System.EventArg e)
{
int cnt= 0;
if( ViewState["ClickCount"] != null)
{
cnt = ViewState["ClickCount"];
}

cnt += 1;
lblClickCount.Text = cnt.ToString();
ViewState["ClickCount"] = cnt

}
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use ajax in asp.net and how to install ajax in my system vel.m8 ASP and ASP.NET Programming 5 04-08-2008 08:55 PM
Is any drawbacks in using VIewState["key"] in forms kingmaker C# Programming 1 08-11-2007 04:59 AM
How to increase viewstate variable in asp .net? oxygen ASP and ASP.NET Programming 1 07-30-2007 09:15 AM
how can i disable teh viewstate in asp .net application ? kingmaker ASP and ASP.NET Programming 1 07-20-2007 06:30 AM
When during the page processing cycle is ViewState available? prasath ASP and ASP.NET Programming 1 07-19-2007 03:29 AM


All times are GMT -7. The time now is 11:04 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0