Refer below sample code.
HTML
<asp:Button Text="Increament counter" OnClick="IncreamentCounter" runat="server" />
Value: <asp:Label ID="lblCount" runat="server" />
Code
C#
protected void IncreamentCounter(object sender, EventArgs e)
{
int counter;
if (ViewState["Count"] != null)
{
counter = Convert.ToInt32(ViewState["Count"]);
}
else
{
counter = 0;
}
if (counter == 500)
{
counter = 1;
}
else
{
counter = counter + 1;
}
ViewState["Count"] = counter;
this.lblCount.Text = counter.ToString();
}
VB.Net
Protected Sub IncreamentCounter(ByVal sender As Object, ByVal e As EventArgs)
Dim counter As Integer
If ViewState("Count") IsNot Nothing Then
counter = Convert.ToInt32(ViewState("Count"))
Else
counter = 0
End If
If counter = 500 Then
counter = 1
Else
counter = counter + 1
End If
ViewState("Count") = counter
Me.lblCount.Text = counter.ToString()
End Sub
Screenshot