473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Componen tModel
Imports System.Web.UI
Imports System.Web.UI.W ebControls

<Description("P rovides a login component"), DefaultProperty (""),
ToolboxData("<{ 0}:Login runat=server></{0}:Login>")> Public Class Login
Inherits System.Web.UI.W ebControls.WebC ontrol
Implements INamingContaine r

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Username() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtUsername "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtUsername "), TextBox).Text = Value
End Set
End Property

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Password() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtPassword "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtPassword "), TextBox).Text = Value
End Set
End Property

Protected Overrides Sub CreateChildCont rols()
With Me.Controls
.Add(New LiteralControl( "<div id=""Panel1""
style=""border-color:Gray;bord er-width:1px;borde r-style:Solid;wid th:200px;"">"))
.Add(New LiteralControl( "<TABLE id=""Table1"" title=""Login""
borderColor=""g ray"" cellSpacing=""2 "" cellPadding=""2 "" width=""300""
bgColor=""gains boro"" border=""0"">") )

'Row 1:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Userna me:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtUsername As New TextBox()
txtUsername.Tex tMode = TextBoxMode.Sin gleLine
txtUsername.ID = "txtUserNam e"
txtUsername.Acc essKey = "U"
txtUsername.Wid th = Unit.Percentage (100)
.Add(txtUsernam e)
.Add(New LiteralControl( "</TD>"))
.Add(New LiteralControl( "</TR>"))
'Row 2:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Passwo rd:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtPass As New TextBox()
txtPass.TextMod e = TextBoxMode.Pas sword
txtPass.ID = "txtPasswor d"
txtPass.AccessK ey = "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:1 00%;""/>"))
.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 "graphicall y/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 1586
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 CreateChildCont rols could be something like:

Protected Overrides Sub CreateChildCont rols()
Controls.Clear( )

'Add table
Dim t As New Table()
t.Width=Unit.Pi xel(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=tex tBoxMode.Passwo rd
...
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************ ***********@yah oo.com> wrote in message
news:uo******** ******@tk2msftn gp13.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.Componen tModel
Imports System.Web.UI
Imports System.Web.UI.W ebControls

<Description("P rovides a login component"), DefaultProperty (""),
ToolboxData("<{ 0}:Login runat=server></{0}:Login>")> Public Class Login
Inherits System.Web.UI.W ebControls.WebC ontrol
Implements INamingContaine r

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Username() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtUsername "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtUsername "), TextBox).Text = Value
End Set
End Property

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Password() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtPassword "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtPassword "), TextBox).Text = Value
End Set
End Property

Protected Overrides Sub CreateChildCont rols()
With Me.Controls
.Add(New LiteralControl( "<div id=""Panel1""
style=""border-color:Gray;bord er-width:1px;borde r-style:Solid;wid th:200px;"">"))
.Add(New LiteralControl( "<TABLE id=""Table1"" title=""Login""
borderColor=""g ray"" cellSpacing=""2 "" cellPadding=""2 "" width=""300""
bgColor=""gains boro"" border=""0"">") )

'Row 1:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Userna me:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtUsername As New TextBox()
txtUsername.Tex tMode = TextBoxMode.Sin gleLine
txtUsername.ID = "txtUserNam e"
txtUsername.Acc essKey = "U"
txtUsername.Wid th = Unit.Percentage (100)
.Add(txtUsernam e)
.Add(New LiteralControl( "</TD>"))
.Add(New LiteralControl( "</TR>"))
'Row 2:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Passwo rd:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtPass As New TextBox()
txtPass.TextMod e = TextBoxMode.Pas sword
txtPass.ID = "txtPasswor d"
txtPass.AccessK ey = "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:1 00%;""/>"))
.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 "graphicall y/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
"graphicall y/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************ ***********@yah oo.com> wrote in message
news:uo******** ******@tk2msftn gp13.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.Componen tModel
Imports System.Web.UI
Imports System.Web.UI.W ebControls

<Description("P rovides a login component"), DefaultProperty (""),
ToolboxData("<{ 0}:Login runat=server></{0}:Login>")> Public Class Login
Inherits System.Web.UI.W ebControls.WebC ontrol
Implements INamingContaine r

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Username() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtUsername "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtUsername "), TextBox).Text = Value
End Set
End Property

<Bindable(True) , Category("Gener al"), DefaultValue("" )> Property
Password() As String
Get
Me.EnsureChildC ontrols()
Return CType(FindContr ol("txtPassword "), TextBox).Text
End Get
Set(ByVal Value As String)
Me.EnsureChildC ontrols()
CType(FindContr ol("txtPassword "), TextBox).Text = Value
End Set
End Property

Protected Overrides Sub CreateChildCont rols()
With Me.Controls
.Add(New LiteralControl( "<div id=""Panel1""
style=""border-color:Gray;bord er-width:1px;borde r-style:Solid;wid th:200px;"">"))
.Add(New LiteralControl( "<TABLE id=""Table1"" title=""Login""
borderColor=""g ray"" cellSpacing=""2 "" cellPadding=""2 "" width=""300""
bgColor=""gains boro"" border=""0"">") )

'Row 1:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Userna me:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtUsername As New TextBox()
txtUsername.Tex tMode = TextBoxMode.Sin gleLine
txtUsername.ID = "txtUserNam e"
txtUsername.Acc essKey = "U"
txtUsername.Wid th = Unit.Percentage (100)
.Add(txtUsernam e)
.Add(New LiteralControl( "</TD>"))
.Add(New LiteralControl( "</TR>"))
'Row 2:
.Add(New LiteralControl( "<TR>"))
'Cell 1:
.Add(New LiteralControl( "<TD>Passwo rd:</TD>"))
'Cell 2:
.Add(New LiteralControl( "<TD width=""200"">" ))
Dim txtPass As New TextBox()
txtPass.TextMod e = TextBoxMode.Pas sword
txtPass.ID = "txtPasswor d"
txtPass.AccessK ey = "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:1 00%;""/>"))
.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 "graphicall y/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************ ***********@yah oo.com> wrote in message
news:er******** ******@TK2MSFTN GP09.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
1586
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 control should display 2 columns and 2 rows per page. I am able to display 4 Composite Controls independently, when the user is changing the index of the DropDownList control, automatcially the output should get reflected on the composite control...
5
2006
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 to the web form. where do I catch the exception at? Example Webform Composite Control
5
4090
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 the child controls appear in the property inspector as sub-properties of the main control and can be manipulated as normal at design time. If any of the properties of the parent (main) control are changed at design time the composite control is...
1
3147
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 many questions. To keep it simple, I'll describe the basics of what I'm trying to design. I've created a TextBox composite control that consists of a label for
2
1570
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 redistribuite them to friends but without having to give away my source code. I'd like to have only one clear, solid dll. Is it possible? Maybe even a similiar way. Thanks to everyone for the attention.
3
2559
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 consituent control caused a postback. for example a have a consituent controls with two buttons on it "button1" and "button2" I have registered my control for postbacks using
4
4089
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 initially configured during DataBind(). Thanks to Steven Cheng for pointing out that you have to set the constituent control properties after you add them to the composite control collection for the restore to work! However, I now have a...
0
1404
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 working on a much more complex grid example, and according to my findings, the order should be like so: Page Constructor Page OnInit
3
3005
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 lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.