Originariamente inviato da djciko
accidenti, mi gira la testa
meglio che vada a pranzo e poi riprendiamo...
ci sono riuscito cmq grazie ai vs consigli...pero' ci sono ancora dei problemi.
a dopo
Visto che TU hai mangiato
ed io sono digiuno
riprendo con un esempio un po' più interessante
Ho un UC che ha un TextBox ed un pulsante. Ad ogni click voglio recuperare il valore del textbox
UserControl:
codice:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="UCriga.ascx.vb" Inherits="corso_apogeo.UCriga" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<tr>
<td>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</td>
</tr>
====================================================
Public Class UCriga
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 TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'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
Public Event onClick(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
End Sub
Public Property Testo$()
Get
Return Me.TextBox1.Text
End Get
Set(ByVal Value$)
Me.TextBox1.Text = Value
End Set
End Property
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent onClick(Me, e)
End Sub
End Class
Pagina chiamante:
codice:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="a.aspx.vb" Inherits="corso_apogeo.a"%>
<%@ Register TagPrefix="uc1" TagName="UCriga" Src="UCriga.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>a</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="../Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="Button1" runat="server" Text="Aggiungi riga"></asp:button>
<asp:button id="Button2" runat="server" Text="Togli riga"></asp:button>
<asp:Button id="Button3" runat="server" Text="Recupero testo"></asp:Button></P>
<asp:linkbutton id="LinkButton1" runat="server">Refresh</asp:linkbutton></P>
<table border="1">
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</table>
</form>
</body>
</HTML>
========================================================
Public Class a
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 Button1 As System.Web.UI.WebControls.Button
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
'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
Private n_righe% = 0
Protected UC As UCriga
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
Dim o As Object = Me.ViewState("n_righe")
If o Is Nothing Then
n_righe = 0
Else
n_righe = CInt(o)
End If
For i As Integer = 1 To n_righe
UC = DirectCast(LoadControl("UCriga.ascx"), UCriga)
UC.ID = "uc_" & i
AddHandler UC.onClick, AddressOf Me.UC_onClick
Me.PlaceHolder1.Controls.Add(UC)
Next
End Sub
Private Sub AggiungiRiga(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UC = DirectCast(LoadControl("UCriga.ascx"), UCriga)
n_righe += 1
Me.ViewState("n_righe") = n_righe
UC.ID = "uc_" & n_righe
AddHandler UC.onClick, AddressOf Me.UC_onClick
Me.PlaceHolder1.Controls.Add(uc)
End Sub
Private Sub TogliRiga(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If n_righe = 0 Then Exit Sub
n_righe -= 1
Me.ViewState("n_righe") = n_righe
Me.PlaceHolder1.Controls.RemoveAt(n_righe)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For i As Integer = 1 To n_righe
PrintLn(DirectCast(Me.FindControl("uc_" & i), UCriga).Testo)
Next
End Sub
Private Sub UC_onClick(ByVal sender As Object, ByVal e As System.EventArgs)
PrintLn(DirectCast(sender, UCriga).Testo)
End Sub
End Class