A
Alex Nitulescu
Hi. Because I'm a beginner in creating controls, I spent more than two *&^#$
hours to create this "login" as a custom control and to make it work
properly:
_________________________________________________________________________________________________________
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<Description("Provides a login component"), DefaultProperty(""),
ToolboxData("<{0}:Login runat=server></{0}:Login>")> Public Class Login
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
<Bindable(True), Category("General"), DefaultValue("")> Property
Username() As String
Get
Me.EnsureChildControls()
Return CType(FindControl("txtUsername"), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildControls()
CType(FindControl("txtUsername"), TextBox).Text = Value
End Set
End Property
<Bindable(True), Category("General"), DefaultValue("")> Property
Password() As String
Get
Me.EnsureChildControls()
Return CType(FindControl("txtPassword"), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildControls()
CType(FindControl("txtPassword"), TextBox).Text = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
With Me.Controls
.Add(New LiteralControl("<div id=""Panel1""
style=""border-color:Gray;border-width:1px;border-style:Solid;width:200px;"">"))
.Add(New LiteralControl("<TABLE id=""Table1"" title=""Login""
borderColor=""gray"" cellSpacing=""2"" cellPadding=""2"" width=""300""
bgColor=""gainsboro"" border=""0"">"))
'Row 1:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD>Username:</TD>"))
'Cell 2:
.Add(New LiteralControl("<TD width=""200"">"))
Dim txtUsername As New TextBox()
txtUsername.TextMode = TextBoxMode.SingleLine
txtUsername.ID = "txtUserName"
txtUsername.AccessKey = "U"
txtUsername.Width = Unit.Percentage(100)
.Add(txtUsername)
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
'Row 2:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD>Password:</TD>"))
'Cell 2:
.Add(New LiteralControl("<TD width=""200"">"))
Dim txtPass As New TextBox()
txtPass.TextMode = TextBoxMode.Password
txtPass.ID = "txtPassword"
txtPass.AccessKey = "P"
txtPass.Width = Unit.Percentage(100)
.Add(txtPass)
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
'Row 3:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD></TD>"))
'Cell 2:
.Add(New LiteralControl("<TD>"))
.Add(New LiteralControl("<input type=""submit""
name=""cmdLogin"" value=""Login"" id=""cmdLogin"" style=""width:100%;""/>"))
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
.Add(New LiteralControl("</TABLE>"))
.Add(New LiteralControl("</div>"))
End With
End Sub
End Class
_________________________________________________________________________________________________________
1. My question is - isn't there a simpler way to do it ? I'm not talking
here about the functionality - I'm talking about writing tons of "add(New
blah blah)" - this is a simple control - imagine what happened if I wanted
to write a much more complicated one, including, for instance, a complex
datagrid, or a repeater with many templates. So... is there any kind of
wizard which allows you to "graphically/visually" design a control as if you
created a regular webform, and then automagically write all this description
code for you ? Is there any other trick of the trade to make this stage any
faster ?
2. I tried before writing the same thing with "<asp:table" instead. I
stumbled on an error saying something like "add 'runat=server' tags to your
controls. Then I got lost, because wherever I tried to put those pesky
'runat=server' tags, it gave me errors. Would you please have a small sample
code creating web tables in custom controls ? Or, maybe, should I have done
someting like:
a) composing my table on some regular webform, somewhere.
b) writing
dim X as Table
c) Setting all the necessary properties, and
d) adding it with
..Add(X)
instead ?
Thank you, Alex.
hours to create this "login" as a custom control and to make it work
properly:
_________________________________________________________________________________________________________
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<Description("Provides a login component"), DefaultProperty(""),
ToolboxData("<{0}:Login runat=server></{0}:Login>")> Public Class Login
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
<Bindable(True), Category("General"), DefaultValue("")> Property
Username() As String
Get
Me.EnsureChildControls()
Return CType(FindControl("txtUsername"), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildControls()
CType(FindControl("txtUsername"), TextBox).Text = Value
End Set
End Property
<Bindable(True), Category("General"), DefaultValue("")> Property
Password() As String
Get
Me.EnsureChildControls()
Return CType(FindControl("txtPassword"), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildControls()
CType(FindControl("txtPassword"), TextBox).Text = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
With Me.Controls
.Add(New LiteralControl("<div id=""Panel1""
style=""border-color:Gray;border-width:1px;border-style:Solid;width:200px;"">"))
.Add(New LiteralControl("<TABLE id=""Table1"" title=""Login""
borderColor=""gray"" cellSpacing=""2"" cellPadding=""2"" width=""300""
bgColor=""gainsboro"" border=""0"">"))
'Row 1:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD>Username:</TD>"))
'Cell 2:
.Add(New LiteralControl("<TD width=""200"">"))
Dim txtUsername As New TextBox()
txtUsername.TextMode = TextBoxMode.SingleLine
txtUsername.ID = "txtUserName"
txtUsername.AccessKey = "U"
txtUsername.Width = Unit.Percentage(100)
.Add(txtUsername)
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
'Row 2:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD>Password:</TD>"))
'Cell 2:
.Add(New LiteralControl("<TD width=""200"">"))
Dim txtPass As New TextBox()
txtPass.TextMode = TextBoxMode.Password
txtPass.ID = "txtPassword"
txtPass.AccessKey = "P"
txtPass.Width = Unit.Percentage(100)
.Add(txtPass)
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
'Row 3:
.Add(New LiteralControl("<TR>"))
'Cell 1:
.Add(New LiteralControl("<TD></TD>"))
'Cell 2:
.Add(New LiteralControl("<TD>"))
.Add(New LiteralControl("<input type=""submit""
name=""cmdLogin"" value=""Login"" id=""cmdLogin"" style=""width:100%;""/>"))
.Add(New LiteralControl("</TD>"))
.Add(New LiteralControl("</TR>"))
.Add(New LiteralControl("</TABLE>"))
.Add(New LiteralControl("</div>"))
End With
End Sub
End Class
_________________________________________________________________________________________________________
1. My question is - isn't there a simpler way to do it ? I'm not talking
here about the functionality - I'm talking about writing tons of "add(New
blah blah)" - this is a simple control - imagine what happened if I wanted
to write a much more complicated one, including, for instance, a complex
datagrid, or a repeater with many templates. So... is there any kind of
wizard which allows you to "graphically/visually" design a control as if you
created a regular webform, and then automagically write all this description
code for you ? Is there any other trick of the trade to make this stage any
faster ?
2. I tried before writing the same thing with "<asp:table" instead. I
stumbled on an error saying something like "add 'runat=server' tags to your
controls. Then I got lost, because wherever I tried to put those pesky
'runat=server' tags, it gave me errors. Would you please have a small sample
code creating web tables in custom controls ? Or, maybe, should I have done
someting like:
a) composing my table on some regular webform, somewhere.
b) writing
dim X as Table
c) Setting all the necessary properties, and
d) adding it with
..Add(X)
instead ?
Thank you, Alex.