Is there a simpler way (trick) to create composite controls ?

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.
 
T

Teemu Keiski

Hi,

definately you don't need to add them as hard-coded LiteralControls but you
can use premade controls. Though there's still adding, it would happen with
premade controls and probably eases the task already.

For example CreateChildControls could be something like:

Protected Overrides Sub CreateChildControls()
Controls.Clear()

'Add table
Dim t As New Table()
t.Width=Unit.Pixel(250)
Controls.Add(t)

'Add row to table
Dim tr As New tableRow()
t.Rows.Add(tr)

'Add a cell to row
Dim td As New TableCell()
tr.cells.Add(td)

*Add TextBox to Cell
Dim tb As New TextBox
tb.ID="textBox1"
tb.TextBode=textBoxMode.Password
...
td.Controls.Add(tb)
...

End Sub

You might want to check this article

Building Composite Controls
http://aspalliance.com/359

What comes to databound controls, they are complex to develop but utilize
many techniques and such which eases the development. Here is a example of
databound control:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcontemplateddataboundcontrolsample.asp

If you generally need a good guide to building controls, I recommend you to
take a look at Developing ASP.NET Server Controls And Components by MSPress.
http://www.microsoft.com/MSPress/books/5728.asp
 
K

Kevin Spencer

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 ?

That would be me. But it will cost you. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
A

Alex Nitulescu

Thank you both for your answers.

And, Kevin....You now what the Chinese say - Give a man a fish and you feed
him for a day. Teach a man to fish and you feed him for a lifetime !
;-)))))))

Alex. :))
 
A

Alan Silver

And, Kevin....You now what the Chinese say - Give a man a fish and you
Or in the modern vernacular ...

Give a man a fish and feed him for a day, teach him to use the Internet
and you won't hear from him for a long while!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top