473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

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.
Nov 19 '05 #1
5 1555
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...trolsample.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

--
Teemu Keiski
ASP.NET MVP, Finland
"Alex Nitulescu" <RE***********************@yahoo.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...
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.

Nov 19 '05 #2
> 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.

"Alex Nitulescu" <RE***********************@yahoo.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl... 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.

Nov 19 '05 #3
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. :-))
Nov 19 '05 #4
LOL!

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

"Alex Nitulescu" <RE***********************@yahoo.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
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. :-))

Nov 19 '05 #5
>> 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 !


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!!

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Satya Bojanapally | last post by:
Hi, I am unable to add a pager for this composite control. I had created a composite control in C#. The control is having 5 labels, one radio button and one DropDownList control. The composite...
5
by: KJ | last post by:
This is kind of hard to explain but I have a The controls are created with CreateChildControls(). Now let say I click a button and an error occurs in the control. If I throw it up it goes back...
5
by: Barry Carr | last post by:
Hi, I've created a composite custom web control and a ControlDesigner descendant to render the control a design time. The child controls are public properties of composite control and as such...
1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
2
by: Crosta | last post by:
Hi everyone, As subject suggests I'm wondering how is it possible to do so in ASP.NET. The fact is that I'd like to "atomize" an entire series of custom user controls (.ascx and .ascx.vb) to...
3
by: Martin | last post by:
Hi, I have created a composite control that has a number of standard asp.net controls on it that can themselves cause postbacks. What i need to do in my composite control is to determine which...
4
by: Mark Olbert | last post by:
This involves a family of related, databound ASPNET2 composite controls. I've managed to arrange things so that the composite controls restore themselves from ViewState on postback after they're...
0
by: multiformity | last post by:
Ok, so I have gone off and documented the lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm Now, I am...
3
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.