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

Discussion in 'ASP .Net' started by Alex Nitulescu, Feb 23, 2005.

  1. 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.
     
    Alex Nitulescu, Feb 23, 2005
    #1
    1. Advertising

  2. Alex Nitulescu

    Teemu Keiski Guest

    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

    --
    Teemu Keiski
    ASP.NET MVP, Finland


    "Alex Nitulescu" <> wrote in message
    news:...
    > 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.
    >
     
    Teemu Keiski, Feb 23, 2005
    #2
    1. Advertising

  3. > 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" <> wrote in message
    news:...
    > 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.
    >
     
    Kevin Spencer, Feb 23, 2005
    #3
  4. 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. :))
     
    Alex Nitulescu, Feb 24, 2005
    #4
  5. LOL!

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

    "Alex Nitulescu" <> wrote in message
    news:...
    > 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. :))
    >
     
    Kevin Spencer, Feb 24, 2005
    #5
  6. Alex Nitulescu

    Alan Silver Guest

    >> 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)
     
    Alan Silver, Mar 3, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,693
    sleigh
    Feb 12, 2004
  2. tmallen
    Replies:
    19
    Views:
    553
    Miles
    Nov 5, 2008
  3. Julian Leviston

    Is there a simpler way to do this?

    Julian Leviston, Aug 17, 2005, in forum: Ruby
    Replies:
    12
    Views:
    230
    Serpent
    Aug 28, 2005
  4. Ritchie Valens

    Is there a simpler way to do this?

    Ritchie Valens, Jul 30, 2011, in forum: Javascript
    Replies:
    6
    Views:
    599
    Scott Sauyet
    Aug 16, 2011
  5. Replies:
    20
    Views:
    376
    Ethan Furman
    Nov 16, 2012
Loading...

Share This Page