Mi da errore. Ecco il codice del mio controllo:
codice:
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Namespace MyControls
Public Class MyLogIn
Inherits Control
Implements INamingContainer
'Creo l'evento OnLogin
Public Event LogIn (s As Object, e As EventArgs)
Protected Sub OnLogIn( e As EventArgs )
RaiseEvent LogIn( Me, EventArgs.Empty )
End Sub
'Creo le proprietà della classe
Private _dataSource As IEnumerable
Public Property DataSource As IEnumerable
Get
return _dataSource
End Get
Set
_dataSource = value
End Set
End Property
Public Property Username As String
Get
Me.EnsureChildControls()
Return CType( Controls( 2 ), TextBox ).Text
End Get
Set
Me.EnsureChildControls()
CType( Controls( 2 ), TextBox ).Text = Value
End Set
End Property
Public Property Password As String
Get
Me.EnsureChildControls()
Return CType( Controls( 5 ), TextBox ).Text
End Get
Set
Me.EnsureChildControls()
CType( Controls( 5 ), TextBox ).Text = Value
End Set
End Property
'Creo i metodi della classe
Sub CheckPassword( s As Object, e As EventArgs )
OnLogIn (EventArgs.Empty)
Dim lblLabel As Label
Dim DataEnum As IEnumerator
lblLabel = CTYPE( Controls( 11 ), Label )
If Not _dataSource Is Nothing
DataEnum = _dataSource.GetEnumerator()
if DataEnum.MoveNext()=false then
lblLabel.text="UserName o password sbagliate"
else
lblLabel.text="Bravo!!!"
end if
End If
End Sub
Protected Overrides Sub CreateChildControls()
Me.Controls.Add( New LiteralControl( "<table><tbody>" ))
Dim Username As new TextBox
Username.id="Username"
Me.Controls.Add( New LiteralControl( "<tr><td>UserName:</td><td>" ) )
Me.Controls.Add( Username )
Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
Dim Password As New TextBox
Password.id="Password"
Password.TextMode = TextBoxMode.Password
Me.Controls.Add( New LiteralControl( "<tr><td>Password:</td><td>" ) )
Me.Controls.Add( Password )
Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
Dim LogInButton As New Button
LogInButton.id="LogIn_Button"
LogInButton.text="LogIn"
AddHandler LogInButton.Click, AddressOf CheckPassword
Me.Controls.Add( New LiteralControl("<tr><td align=""middle"" colspan=""2"">"))
Me.Controls.Add( LogInButton )
Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
Dim lblLabel As New Label
lblLabel.EnableViewState = False
Me.Controls.Add( New LiteralControl("<tr><td align=""middle"" colspan=""2"">"))
Me.Controls.Add( lblLabel )
Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
Me.Controls.Add( New LiteralControl( "</tbody></table>" ))
End Sub
End Class
End Namespace
Ho visto però che se modfico il codice in questa maniera:
codice:
...
'Creo i metodi della classe
Sub CheckPassword( s As Object, e As EventArgs )
OnLogIn (EventArgs.Empty)
End Sub
Protected Overrides Sub OnDataBinding( e As EventArgs )
Dim lblLabel As Label
Dim DataEnum As IEnumerator
lblLabel = CTYPE( Controls( 11 ), Label )
If Not _dataSource Is Nothing
DataEnum = _dataSource.GetEnumerator()
if DataEnum.MoveNext()=false then
lblLabel.text="UserName o password sbagliate"
else
lblLabel.text="Bravo!!!"
end if
End If
End Sub
...
posso tranquillamente chiudere connessione e datareader (e forse è anche più corretto...). Solo non capisco perchè nell'altro modo mi da l'errore e così no :master: