Ti faccio un esempio semplice
user control WebUserControl1.ascx
codice:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.vb" Inherits="WebApplication1.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:LinkButton id="LinkButton_indietro" runat="server">Indietro</asp:LinkButton>
<asp:label id="Label_numero" runat="server"></asp:label>
<asp:linkbutton id="LinkButton_avanti" runat="server">Avanti</asp:linkbutton>
codice user control
codice:
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
#Region " Codice generato da Progettazione Web Form "
'Chiamata richiesta da Progettazione Web Form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents LinkButton_avanti As System.Web.UI.WebControls.LinkButton
Protected WithEvents Label_numero As System.Web.UI.WebControls.Label
Protected WithEvents LinkButton_indietro As System.Web.UI.WebControls.LinkButton
'NOTA: la seguente dichiarazione è richiesta da Progettazione Web Form.
'Non spostarla o rimuoverla.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: questa chiamata al metodo è richiesta da Progettazione Web Form.
'Non modificarla nell'editor del codice.
InitializeComponent()
End Sub
#End Region
'definisco l'evento che si scatena al cambio del numero
Public Event NumberChanged(ByVal sender As Object, ByVal e As EventArgs)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Inserire qui il codice utente necessario per inizializzare la pagina
Me.Label_numero.Text = numero
End Sub
'definisco un proprità pubblica numero, intero, >= 0, predefinito = 0
Private _numero As Integer = Integer.MinValue 'valore sicuramente non valido
Public Property numero() As Integer
Get
'se il numero non è valido
If Me._numero = Integer.MinValue Then
'ripristino il viewstate solo quando la proprietà viene letta per la prima volta
Dim o As Object = Me.ViewState("numero")
If Not (o Is Nothing) Then
Me._numero = CInt(o)
Else
Me._numero = 0 'valore predefinito
End If
End If
Return Me._numero
End Get
Set(ByVal Value As Integer)
If Value >= 0 Then
Me._numero = Value
'lo salvo
Me.ViewState("numero") = Value
'aggiorno la label
Me.Label_numero.Text = Value.ToString
'scateno l'evento
RaiseEvent NumberChanged(Me, EventArgs.Empty)
End If
End Set
End Property
Private Sub LinkButton_avanti_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton_avanti.Click
numero += 1
End Sub
Private Sub LinkButton_indietro_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton_indietro.Click
If numero > 0 Then numero -= 1
End Sub
End Class
pagina che utilizza l'user control
codice:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="Form1" method="post" runat="server">
<uc1:webusercontrol1 id="WebUserControl11" runat="server"></uc1:webusercontrol1>
</p>
<p id="p1" runat="server"></p>
<asp:linkbutton id="LinkButton1" runat="server">Refresh</asp:linkbutton></p>
</form>
</body>
</html>
codice
codice:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Codice generato da Progettazione Web Form "
'Chiamata richiesta da Progettazione Web Form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents p1 As System.Web.UI.HtmlControls.HtmlGenericControl
'NOTA: la seguente dichiarazione è richiesta da Progettazione Web Form.
'Non spostarla o rimuoverla.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: questa chiamata al metodo è richiesta da Progettazione Web Form.
'Non modificarla nell'editor del codice.
InitializeComponent()
End Sub
#End Region
Protected WithEvents WebUserControl11 As WebUserControl1
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'imposto la proprietà numero dell'user control
If Not Me.IsPostBack Then
Me.WebUserControl11.numero = 10
End If
End Sub
Private Sub WebUserControl11_NumberChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebUserControl11.NumberChanged
'intercetto l'evento dell'user control
Me.p1.InnerText = String.Format("Ho cambiato il numero: {0}", Me.WebUserControl11.numero)
End Sub
End Class