Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retaining a value
Message
 
 
To
22/10/2009 17:10:42
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01430818
Message ID:
01430820
Views:
38
>I put a label and a button on a form. Every time I click the button, I want the value of the label to increment by one.
>I've gone through several attempts to accomplish this, but they all lead to setting the label to the same value everytime the button is pressed. The latest attempt was
>
>Partial Class Default2
>    Inherits System.Web.UI.Page
> 
>    Function increment_counter() As Integer
>        Static iCounter As Integer = 0
>        iCounter = iCounter + 1
>        Return iCounter
>    End Function
>
>    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
>        Label1.Text = increment_counter()
>    End Sub
>End Class
>
>
>I'm sure there's something fundemental I'm missing about the difference between Web and desktop computing....almost certainly something to do with stateless computing......but if somebody could post some sample code, I can use that as a jumping off point.
>
>In case it's relevant, here's the ASPX portion of the code
>< %@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
>
>< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>
>< html xmlns="http://www.w3.org/1999/xhtml">
>< head runat="server">
>    < title>< /title>
>< /head>
>< body>
>    < form id="form1" runat="server">
>    < div>
>    
>    < /div>
>    < asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
>            
>    < asp:Button ID="Button1" runat="server" Text="Button" />
>    < /form>
>< /body>
>< /html>
>
>Thanks to all.........Rich

You need to save current value either in Session or in a ViewState and use it.

Also, would it work if

Label.Text = Convert.ToInt32(Label.Text) + 1
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform