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,
Nitin Pawar
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(ByVal senderAs Object,ByVal eAs System.EventArgs)Dim cntAs Integer = 0If Not ViewState("ClickCount")Is Nothing Then cnt =CInt(ViewState("ClickCount"))End If cnt += 1 lblClickCount.Text = cnt.ToString() ViewState("ClickCount") = cntEnd Sub
No comments:
Post a Comment