Riporto di seguito una prova fatta su una banalissima application Web (quella su cui sto lavorando è troppo lunga per riportarla tutta): la pressione del button incrementa un contatore (lato server) che voglio visualizzare nel testo della Label (lato client).
Cosa sbaglio?![]()
Pagina Default.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>Prova di passaggio varibili lato server -> lato client</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text='<%= Session("Conta") %>'></asp:Label>
</div>
</form>
</body>
</html>
Pagina Default.aspx.vb:
Partial Class _Default
Inherits System.Web.UI.Page
Dim count As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then ' Primo avvio dell'Application Web
Session("Conta") = "0"
Label1.Text = Session("Conta")
Else ' PostBack causato dal click sul Button
count = CType(Session("Conta"), String)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
count = Integer.Parse(Session("Conta")) + 1
Session("Conta") = count.ToString
End Sub
End Class