Dynamically adding custom control

Discussion in 'ASP .Net Building Controls' started by Terry Holland, Nov 17, 2005.

  1. Im trying to dynamically add a user control for a webform but it is not
    appearing when I run the form.

    I have pasted the code that I am using. Can someone advise what Im doing
    wrong. My user control (ctlTextBox) contains one textbox. When I dragged
    this onto the control, the line

    Protected WithEvents txt As System.Web.UI.WebControls.TextBox

    was added the declarations area of the code module. Without changing this
    to

    Protected WithEvents txt As New System.Web.UI.WebControls.TextBox

    An error is generated on my page as soon as I hit the line

    .Text = "Tesing Control"

    Also, if I drag ctlTextBox onto my form at design time, it displays OK


    ==========================
    User Control Code
    ==========================
    Public Class ctlTextBox
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    '############################################################
    '## This is the code that was generated
    ############
    '## but without the New keyword, a runtime error is generated
    ############
    '## when I try to set the Text property on the page
    ############
    '############################################################
    'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    Protected WithEvents txt As New System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Public Property Width() As Integer
    Get
    Return txt.Width.Value
    End Get
    Set(ByVal Value As Integer)
    txt.Width = Unit.Pixel(Value)
    End Set
    End Property

    Public Property Text() As String
    Get
    Return txt.Text
    End Get
    Set(ByVal Value As String)
    txt.Text = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    txt.CssClass = "Editable"
    End Sub

    End Class
    ===========================
    End User Control Code
    ===========================


    ===========================
    Page Code
    ===========================
    Public Class pgeTest
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    BuildControls()
    End Sub


    Sub BuildControls()
    Dim tb As New ctlTextBox
    With tb
    .Text = "Tesing Control"
    End With
    Panel1.Controls.Add(tb)

    Dim txt As New TextBox
    txt.Text = "Hello"
    Panel1.Controls.Add(txt)

    Dim lbl As New Label
    lbl.Text = "Label"
    Panel1.Controls.Add(lbl)

    End Sub

    End Class
    ===============================
    End of Page Code
    ===============================
     
    Terry Holland, Nov 17, 2005
    #1
    1. Advertising

  2. Hi Terry,

    As for ASP.NET Usercontrol(ascx...), the Control instances are all defined
    in the .ascx template file(if not dynamically adding control...), so we
    should not declare "New" for control reference like:

    Protected WithEvents txt As New System.Web.UI.WebControls.TextBox

    If you get error when not using "New", there must have something incorrect
    in our page or code logic. Would you also provide the ascx template
    together with the code behind?

    In addition, for dynamically adding Usercontrol onto aspx page, we should
    use the Page.LoadControl method to load the "xxx.ascx" so as to create the
    UserControl instance and add it into Container control in each Page
    Request....

    If there're anything unclear, please feel free to post here.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    --------------------
    | From: "Terry Holland" <>
    | Subject: Dynamically adding custom control
    | Date: Thu, 17 Nov 2005 21:28:24 -0000
    | Lines: 149
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | Im trying to dynamically add a user control for a webform but it is not
    | appearing when I run the form.
    |
    | I have pasted the code that I am using. Can someone advise what Im doing
    | wrong. My user control (ctlTextBox) contains one textbox. When I
    dragged
    | this onto the control, the line
    |
    | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    |
    | was added the declarations area of the code module. Without changing
    this
    | to
    |
    | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    |
    | An error is generated on my page as soon as I hit the line
    |
    | .Text = "Tesing Control"
    |
    | Also, if I drag ctlTextBox onto my form at design time, it displays OK
    |
    |
    | ==========================
    | User Control Code
    | ==========================
    | Public Class ctlTextBox
    | Inherits System.Web.UI.UserControl
    |
    | #Region " Web Form Designer Generated Code "
    |
    | 'This call is required by the Web Form Designer.
    | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | InitializeComponent()
    |
    | End Sub
    |
    | '############################################################
    | '## This is the code that was generated
    | ############
    | '## but without the New keyword, a runtime error is generated
    | ############
    | '## when I try to set the Text property on the page
    | ############
    | '############################################################
    | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    |
    | 'NOTE: The following placeholder declaration is required by the Web
    Form
    | Designer.
    | 'Do not delete or move it.
    | Private designerPlaceholderDeclaration As System.Object
    |
    | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Init
    | 'CODEGEN: This method call is required by the Web Form Designer
    | 'Do not modify it using the code editor.
    | InitializeComponent()
    | End Sub
    |
    | #End Region
    |
    | Public Property Width() As Integer
    | Get
    | Return txt.Width.Value
    | End Get
    | Set(ByVal Value As Integer)
    | txt.Width = Unit.Pixel(Value)
    | End Set
    | End Property
    |
    | Public Property Text() As String
    | Get
    | Return txt.Text
    | End Get
    | Set(ByVal Value As String)
    | txt.Text = Value
    | End Set
    | End Property
    |
    | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    | 'Put user code to initialize the page here
    | txt.CssClass = "Editable"
    | End Sub
    |
    | End Class
    | ===========================
    | End User Control Code
    | ===========================
    |
    |
    | ===========================
    | Page Code
    | ===========================
    | Public Class pgeTest
    | Inherits System.Web.UI.Page
    |
    | #Region " Web Form Designer Generated Code "
    |
    | 'This call is required by the Web Form Designer.
    | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | InitializeComponent()
    |
    | End Sub
    |
    | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    |
    | 'NOTE: The following placeholder declaration is required by the Web
    Form
    | Designer.
    | 'Do not delete or move it.
    | Private designerPlaceholderDeclaration As System.Object
    |
    | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Init
    | 'CODEGEN: This method call is required by the Web Form Designer
    | 'Do not modify it using the code editor.
    | InitializeComponent()
    | End Sub
    |
    | #End Region
    |
    | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    | BuildControls()
    | End Sub
    |
    |
    | Sub BuildControls()
    | Dim tb As New ctlTextBox
    | With tb
    | .Text = "Tesing Control"
    | End With
    | Panel1.Controls.Add(tb)
    |
    | Dim txt As New TextBox
    | txt.Text = "Hello"
    | Panel1.Controls.Add(txt)
    |
    | Dim lbl As New Label
    | lbl.Text = "Label"
    | Panel1.Controls.Add(lbl)
    |
    | End Sub
    |
    | End Class
    | ===============================
    | End of Page Code
    | ===============================
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 18, 2005
    #2
    1. Advertising

  3. We meet again Steven.

    Ive modified my code to remove the New keyword and added the LoadControl
    method in my page to load the control and now the user control appears on
    the screen.

    Thanks

    Terry Holland


    "Steven Cheng[MSFT]" <> wrote in message
    news:jlvfhG$...
    > Hi Terry,
    >
    > As for ASP.NET Usercontrol(ascx...), the Control instances are all defined
    > in the .ascx template file(if not dynamically adding control...), so we
    > should not declare "New" for control reference like:
    >
    > Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    >
    > If you get error when not using "New", there must have something incorrect
    > in our page or code logic. Would you also provide the ascx template
    > together with the code behind?
    >
    > In addition, for dynamically adding Usercontrol onto aspx page, we should
    > use the Page.LoadControl method to load the "xxx.ascx" so as to create the
    > UserControl instance and add it into Container control in each Page
    > Request....
    >
    > If there're anything unclear, please feel free to post here.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    > --------------------
    > | From: "Terry Holland" <>
    > | Subject: Dynamically adding custom control
    > | Date: Thu, 17 Nov 2005 21:28:24 -0000
    > | Lines: 149
    > | X-Priority: 3
    > | X-MSMail-Priority: Normal
    > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | X-RFC2646: Format=Flowed; Original
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | Message-ID: <#>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > |
    > | Im trying to dynamically add a user control for a webform but it is not
    > | appearing when I run the form.
    > |
    > | I have pasted the code that I am using. Can someone advise what Im
    > doing
    > | wrong. My user control (ctlTextBox) contains one textbox. When I
    > dragged
    > | this onto the control, the line
    > |
    > | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    > |
    > | was added the declarations area of the code module. Without changing
    > this
    > | to
    > |
    > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    > |
    > | An error is generated on my page as soon as I hit the line
    > |
    > | .Text = "Tesing Control"
    > |
    > | Also, if I drag ctlTextBox onto my form at design time, it displays OK
    > |
    > |
    > | ==========================
    > | User Control Code
    > | ==========================
    > | Public Class ctlTextBox
    > | Inherits System.Web.UI.UserControl
    > |
    > | #Region " Web Form Designer Generated Code "
    > |
    > | 'This call is required by the Web Form Designer.
    > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | InitializeComponent()
    > |
    > | End Sub
    > |
    > | '############################################################
    > | '## This is the code that was generated
    > | ############
    > | '## but without the New keyword, a runtime error is generated
    > | ############
    > | '## when I try to set the Text property on the page
    > | ############
    > | '############################################################
    > | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    > |
    > | 'NOTE: The following placeholder declaration is required by the Web
    > Form
    > | Designer.
    > | 'Do not delete or move it.
    > | Private designerPlaceholderDeclaration As System.Object
    > |
    > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | System.EventArgs) Handles MyBase.Init
    > | 'CODEGEN: This method call is required by the Web Form Designer
    > | 'Do not modify it using the code editor.
    > | InitializeComponent()
    > | End Sub
    > |
    > | #End Region
    > |
    > | Public Property Width() As Integer
    > | Get
    > | Return txt.Width.Value
    > | End Get
    > | Set(ByVal Value As Integer)
    > | txt.Width = Unit.Pixel(Value)
    > | End Set
    > | End Property
    > |
    > | Public Property Text() As String
    > | Get
    > | Return txt.Text
    > | End Get
    > | Set(ByVal Value As String)
    > | txt.Text = Value
    > | End Set
    > | End Property
    > |
    > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | System.EventArgs) Handles MyBase.Load
    > | 'Put user code to initialize the page here
    > | txt.CssClass = "Editable"
    > | End Sub
    > |
    > | End Class
    > | ===========================
    > | End User Control Code
    > | ===========================
    > |
    > |
    > | ===========================
    > | Page Code
    > | ===========================
    > | Public Class pgeTest
    > | Inherits System.Web.UI.Page
    > |
    > | #Region " Web Form Designer Generated Code "
    > |
    > | 'This call is required by the Web Form Designer.
    > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | InitializeComponent()
    > |
    > | End Sub
    > |
    > | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    > |
    > | 'NOTE: The following placeholder declaration is required by the Web
    > Form
    > | Designer.
    > | 'Do not delete or move it.
    > | Private designerPlaceholderDeclaration As System.Object
    > |
    > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | System.EventArgs) Handles MyBase.Init
    > | 'CODEGEN: This method call is required by the Web Form Designer
    > | 'Do not modify it using the code editor.
    > | InitializeComponent()
    > | End Sub
    > |
    > | #End Region
    > |
    > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | System.EventArgs) Handles MyBase.Load
    > | BuildControls()
    > | End Sub
    > |
    > |
    > | Sub BuildControls()
    > | Dim tb As New ctlTextBox
    > | With tb
    > | .Text = "Tesing Control"
    > | End With
    > | Panel1.Controls.Add(tb)
    > |
    > | Dim txt As New TextBox
    > | txt.Text = "Hello"
    > | Panel1.Controls.Add(txt)
    > |
    > | Dim lbl As New Label
    > | lbl.Text = "Label"
    > | Panel1.Controls.Add(lbl)
    > |
    > | End Sub
    > |
    > | End Class
    > | ===============================
    > | End of Page Code
    > | ===============================
    > |
    > |
    > |
    >
     
    Terry Holland, Nov 18, 2005
    #3
  4. You're welcome Terry,

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | From: "Terry Holland" <>
    | References: <#>
    <jlvfhG$>
    | Subject: Re: Dynamically adding custom control
    | Date: Fri, 18 Nov 2005 15:12:33 -0000
    | Lines: 218
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: host225.multiserv.com 194.200.135.225
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:14006
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | We meet again Steven.
    |
    | Ive modified my code to remove the New keyword and added the LoadControl
    | method in my page to load the control and now the user control appears on
    | the screen.
    |
    | Thanks
    |
    | Terry Holland
    |
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:jlvfhG$...
    | > Hi Terry,
    | >
    | > As for ASP.NET Usercontrol(ascx...), the Control instances are all
    defined
    | > in the .ascx template file(if not dynamically adding control...), so we
    | > should not declare "New" for control reference like:
    | >
    | > Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | >
    | > If you get error when not using "New", there must have something
    incorrect
    | > in our page or code logic. Would you also provide the ascx template
    | > together with the code behind?
    | >
    | > In addition, for dynamically adding Usercontrol onto aspx page, we
    should
    | > use the Page.LoadControl method to load the "xxx.ascx" so as to create
    the
    | > UserControl instance and add it into Container control in each Page
    | > Request....
    | >
    | > If there're anything unclear, please feel free to post here.
    | >
    | > Regards,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | >
    | > --------------------
    | > | From: "Terry Holland" <>
    | > | Subject: Dynamically adding custom control
    | > | Date: Thu, 17 Nov 2005 21:28:24 -0000
    | > | Lines: 149
    | > | X-Priority: 3
    | > | X-MSMail-Priority: Normal
    | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | > | X-RFC2646: Format=Flowed; Original
    | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | > | Message-ID: <#>
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > |
    | > | Im trying to dynamically add a user control for a webform but it is
    not
    | > | appearing when I run the form.
    | > |
    | > | I have pasted the code that I am using. Can someone advise what Im
    | > doing
    | > | wrong. My user control (ctlTextBox) contains one textbox. When I
    | > dragged
    | > | this onto the control, the line
    | > |
    | > | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    | > |
    | > | was added the declarations area of the code module. Without changing
    | > this
    | > | to
    | > |
    | > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | > |
    | > | An error is generated on my page as soon as I hit the line
    | > |
    | > | .Text = "Tesing Control"
    | > |
    | > | Also, if I drag ctlTextBox onto my form at design time, it displays OK
    | > |
    | > |
    | > | ==========================
    | > | User Control Code
    | > | ==========================
    | > | Public Class ctlTextBox
    | > | Inherits System.Web.UI.UserControl
    | > |
    | > | #Region " Web Form Designer Generated Code "
    | > |
    | > | 'This call is required by the Web Form Designer.
    | > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | > | InitializeComponent()
    | > |
    | > | End Sub
    | > |
    | > | '############################################################
    | > | '## This is the code that was generated
    | > | ############
    | > | '## but without the New keyword, a runtime error is generated
    | > | ############
    | > | '## when I try to set the Text property on the page
    | > | ############
    | > | '############################################################
    | > | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    | > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | > |
    | > | 'NOTE: The following placeholder declaration is required by the
    Web
    | > Form
    | > | Designer.
    | > | 'Do not delete or move it.
    | > | Private designerPlaceholderDeclaration As System.Object
    | > |
    | > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | > | System.EventArgs) Handles MyBase.Init
    | > | 'CODEGEN: This method call is required by the Web Form
    Designer
    | > | 'Do not modify it using the code editor.
    | > | InitializeComponent()
    | > | End Sub
    | > |
    | > | #End Region
    | > |
    | > | Public Property Width() As Integer
    | > | Get
    | > | Return txt.Width.Value
    | > | End Get
    | > | Set(ByVal Value As Integer)
    | > | txt.Width = Unit.Pixel(Value)
    | > | End Set
    | > | End Property
    | > |
    | > | Public Property Text() As String
    | > | Get
    | > | Return txt.Text
    | > | End Get
    | > | Set(ByVal Value As String)
    | > | txt.Text = Value
    | > | End Set
    | > | End Property
    | > |
    | > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | > | System.EventArgs) Handles MyBase.Load
    | > | 'Put user code to initialize the page here
    | > | txt.CssClass = "Editable"
    | > | End Sub
    | > |
    | > | End Class
    | > | ===========================
    | > | End User Control Code
    | > | ===========================
    | > |
    | > |
    | > | ===========================
    | > | Page Code
    | > | ===========================
    | > | Public Class pgeTest
    | > | Inherits System.Web.UI.Page
    | > |
    | > | #Region " Web Form Designer Generated Code "
    | > |
    | > | 'This call is required by the Web Form Designer.
    | > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | > | InitializeComponent()
    | > |
    | > | End Sub
    | > |
    | > | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    | > |
    | > | 'NOTE: The following placeholder declaration is required by the
    Web
    | > Form
    | > | Designer.
    | > | 'Do not delete or move it.
    | > | Private designerPlaceholderDeclaration As System.Object
    | > |
    | > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | > | System.EventArgs) Handles MyBase.Init
    | > | 'CODEGEN: This method call is required by the Web Form
    Designer
    | > | 'Do not modify it using the code editor.
    | > | InitializeComponent()
    | > | End Sub
    | > |
    | > | #End Region
    | > |
    | > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | > | System.EventArgs) Handles MyBase.Load
    | > | BuildControls()
    | > | End Sub
    | > |
    | > |
    | > | Sub BuildControls()
    | > | Dim tb As New ctlTextBox
    | > | With tb
    | > | .Text = "Tesing Control"
    | > | End With
    | > | Panel1.Controls.Add(tb)
    | > |
    | > | Dim txt As New TextBox
    | > | txt.Text = "Hello"
    | > | Panel1.Controls.Add(txt)
    | > |
    | > | Dim lbl As New Label
    | > | lbl.Text = "Label"
    | > | Panel1.Controls.Add(lbl)
    | > |
    | > | End Sub
    | > |
    | > | End Class
    | > | ===============================
    | > | End of Page Code
    | > | ===============================
    | > |
    | > |
    | > |
    | >
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 19, 2005
    #4
  5. Terry Holland

    DS Guest

    Unable to access my user control.

    Hi steven,

    I have a similar problem as Terry. I have a user control. I add this user
    control to a ASP:TABLE in my aspx page in the code behind using LoadControl
    method n number of times. When the page is rendered, i could see the all the
    n copies of my user control. I have a submit button in my page, when I click
    this button I post back to the same page but do a different functionality.
    Now the problem is I am not able to access any of the usercontrols. How do I
    refer to my usercontrol and the textboxes inside my user control.

    Need ur help.
    D. Santhosh

    "Steven Cheng[MSFT]" wrote:

    > You're welcome Terry,
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    > --------------------
    > | From: "Terry Holland" <>
    > | References: <#>
    > <jlvfhG$>
    > | Subject: Re: Dynamically adding custom control
    > | Date: Fri, 18 Nov 2005 15:12:33 -0000
    > | Lines: 218
    > | X-Priority: 3
    > | X-MSMail-Priority: Normal
    > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | X-RFC2646: Format=Flowed; Original
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | Message-ID: <>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | NNTP-Posting-Host: host225.multiserv.com 194.200.135.225
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.buildingcontrols:14006
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > |
    > | We meet again Steven.
    > |
    > | Ive modified my code to remove the New keyword and added the LoadControl
    > | method in my page to load the control and now the user control appears on
    > | the screen.
    > |
    > | Thanks
    > |
    > | Terry Holland
    > |
    > |
    > | "Steven Cheng[MSFT]" <> wrote in message
    > | news:jlvfhG$...
    > | > Hi Terry,
    > | >
    > | > As for ASP.NET Usercontrol(ascx...), the Control instances are all
    > defined
    > | > in the .ascx template file(if not dynamically adding control...), so we
    > | > should not declare "New" for control reference like:
    > | >
    > | > Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    > | >
    > | > If you get error when not using "New", there must have something
    > incorrect
    > | > in our page or code logic. Would you also provide the ascx template
    > | > together with the code behind?
    > | >
    > | > In addition, for dynamically adding Usercontrol onto aspx page, we
    > should
    > | > use the Page.LoadControl method to load the "xxx.ascx" so as to create
    > the
    > | > UserControl instance and add it into Container control in each Page
    > | > Request....
    > | >
    > | > If there're anything unclear, please feel free to post here.
    > | >
    > | > Regards,
    > | >
    > | > Steven Cheng
    > | > Microsoft Online Support
    > | >
    > | > Get Secure! www.microsoft.com/security
    > | > (This posting is provided "AS IS", with no warranties, and confers no
    > | > rights.)
    > | >
    > | >
    > | > --------------------
    > | > | From: "Terry Holland" <>
    > | > | Subject: Dynamically adding custom control
    > | > | Date: Thu, 17 Nov 2005 21:28:24 -0000
    > | > | Lines: 149
    > | > | X-Priority: 3
    > | > | X-MSMail-Priority: Normal
    > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | > | X-RFC2646: Format=Flowed; Original
    > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | > | Message-ID: <#>
    > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | > | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    > | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    > | > | Xref: TK2MSFTNGXA02.phx.gbl
    > | > microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | > |
    > | > | Im trying to dynamically add a user control for a webform but it is
    > not
    > | > | appearing when I run the form.
    > | > |
    > | > | I have pasted the code that I am using. Can someone advise what Im
    > | > doing
    > | > | wrong. My user control (ctlTextBox) contains one textbox. When I
    > | > dragged
    > | > | this onto the control, the line
    > | > |
    > | > | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    > | > |
    > | > | was added the declarations area of the code module. Without changing
    > | > this
    > | > | to
    > | > |
    > | > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    > | > |
    > | > | An error is generated on my page as soon as I hit the line
    > | > |
    > | > | .Text = "Tesing Control"
    > | > |
    > | > | Also, if I drag ctlTextBox onto my form at design time, it displays OK
    > | > |
    > | > |
    > | > | ==========================
    > | > | User Control Code
    > | > | ==========================
    > | > | Public Class ctlTextBox
    > | > | Inherits System.Web.UI.UserControl
    > | > |
    > | > | #Region " Web Form Designer Generated Code "
    > | > |
    > | > | 'This call is required by the Web Form Designer.
    > | > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | > | InitializeComponent()
    > | > |
    > | > | End Sub
    > | > |
    > | > | '############################################################
    > | > | '## This is the code that was generated
    > | > | ############
    > | > | '## but without the New keyword, a runtime error is generated
    > | > | ############
    > | > | '## when I try to set the Text property on the page
    > | > | ############
    > | > | '############################################################
    > | > | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    > | > | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    > | > |
    > | > | 'NOTE: The following placeholder declaration is required by the
    > Web
    > | > Form
    > | > | Designer.
    > | > | 'Do not delete or move it.
    > | > | Private designerPlaceholderDeclaration As System.Object
    > | > |
    > | > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | > | System.EventArgs) Handles MyBase.Init
    > | > | 'CODEGEN: This method call is required by the Web Form
    > Designer
    > | > | 'Do not modify it using the code editor.
    > | > | InitializeComponent()
    > | > | End Sub
    > | > |
    > | > | #End Region
    > | > |
    > | > | Public Property Width() As Integer
    > | > | Get
    > | > | Return txt.Width.Value
    > | > | End Get
    > | > | Set(ByVal Value As Integer)
    > | > | txt.Width = Unit.Pixel(Value)
    > | > | End Set
    > | > | End Property
    > | > |
    > | > | Public Property Text() As String
    > | > | Get
    > | > | Return txt.Text
    > | > | End Get
    > | > | Set(ByVal Value As String)
    > | > | txt.Text = Value
    > | > | End Set
    > | > | End Property
    > | > |
    > | > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | > | System.EventArgs) Handles MyBase.Load
    > | > | 'Put user code to initialize the page here
    > | > | txt.CssClass = "Editable"
    > | > | End Sub
    > | > |
    > | > | End Class
    > | > | ===========================
    > | > | End User Control Code
    > | > | ===========================
    > | > |
    > | > |
    > | > | ===========================
    > | > | Page Code
    > | > | ===========================
    > | > | Public Class pgeTest
    > | > | Inherits System.Web.UI.Page
    > | > |
    > | > | #Region " Web Form Designer Generated Code "
    > | > |
    > | > | 'This call is required by the Web Form Designer.
    > | > | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | > | InitializeComponent()
    > | > |
    > | > | End Sub
    > | > |
    > | > | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    > | > |
    > | > | 'NOTE: The following placeholder declaration is required by the
    > Web
    > | > Form
    > | > | Designer.
    > | > | 'Do not delete or move it.
    > | > | Private designerPlaceholderDeclaration As System.Object
    > | > |
    > | > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | > | System.EventArgs) Handles MyBase.Init
    > | > | 'CODEGEN: This method call is required by the Web Form
    > Designer
    > | > | 'Do not modify it using the code editor.
    > | > | InitializeComponent()
    > | > | End Sub
    > | > |
    > | > | #End Region
    > | > |
    > | > | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | > | System.EventArgs) Handles MyBase.Load
    > | > | BuildControls()
    > | > | End Sub
    > | > |
    > | > |
    > | > | Sub BuildControls()
    > | > | Dim tb As New ctlTextBox
    > | > | With tb
    > | > | .Text = "Tesing Control"
    > | > | End With
    > | > | Panel1.Controls.Add(tb)
    > | > |
    > | > | Dim txt As New TextBox
    > | > | txt.Text = "Hello"
    > | > | Panel1.Controls.Add(txt)
    > | > |
    > | > | Dim lbl As New Label
    > | > | lbl.Text = "Label"
    > | > | Panel1.Controls.Add(lbl)
    > | > |
    > | > | End Sub
    > | > |
    > | > | End Class
    > | > | ===============================
    > | > | End of Page Code
    > | > | ===============================
    > | > |
    > | > |
    > | > |
    > | >
    > |
    > |
    > |
    >
    >
     
    DS, Nov 22, 2005
    #5
  6. Steven

    Im getting in a bit of a mess with this. What I want to end up with is a
    set of controls for editing / displaying fields on a webform. I would like
    to be able to dynamically add these controls to webforms as and when I need
    them.

    Initially I need three types of fields

    ctlFieldReadOnly
    ctlFieldEditable
    ctlFieldEditableRequired

    Each of these controls will consist of the following controls:

    ctlFieldReadOnly: Label (lblLabel) and a label (lblValue) to display the
    data
    ctlFieldEditable: Label (lblLabel) and a textbox (txtValue - background
    colour blue) to the display the data
    ctlFieldEditableRequired: Label (lblLabel) and a textbox (txtValue -
    background colour yellow) to the display the data.

    Each of these controls needs to expose a Label property and a Value
    property.
    Each control should be able to be configured so the either, the Label
    control is above the Value control, or the Label control appears to the Left
    of the Value control.

    Each form may contain any number of each type of control.

    Ive been trying to develop along the following lines:
    Create a Base control such as ctlField that must be inherited. This control
    will contain the properties Label, Value, Width and HorizontalAlignment.
    Im then trying to derive the required controls from this control. My derived
    controls should add the relevant webcontrols ie Label to display the Value
    for a ctlReadOnly control, a Textbox to display the Value for
    ctlFieldEditable & ctlFieldEditableRequired controls.
    Im getting various results ranging from no controls being displayed, not
    being able to refer to controls in order to read/write values.

    It would be very helpful if you could provide a small sample of how I might
    get this sort of scenario to work (VB if possible - I know you prefer C#)


    Terry



    "Terry Holland" <> wrote in message
    news:...
    > We meet again Steven.
    >
    > Ive modified my code to remove the New keyword and added the LoadControl
    > method in my page to load the control and now the user control appears on
    > the screen.
    >
    > Thanks
    >
    > Terry Holland
    >
    >
    > "Steven Cheng[MSFT]" <> wrote in message
    > news:jlvfhG$...
    >> Hi Terry,
    >>
    >> As for ASP.NET Usercontrol(ascx...), the Control instances are all
    >> defined
    >> in the .ascx template file(if not dynamically adding control...), so we
    >> should not declare "New" for control reference like:
    >>
    >> Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    >>
    >> If you get error when not using "New", there must have something
    >> incorrect
    >> in our page or code logic. Would you also provide the ascx template
    >> together with the code behind?
    >>
    >> In addition, for dynamically adding Usercontrol onto aspx page, we should
    >> use the Page.LoadControl method to load the "xxx.ascx" so as to create
    >> the
    >> UserControl instance and add it into Container control in each Page
    >> Request....
    >>
    >> If there're anything unclear, please feel free to post here.
    >>
    >> Regards,
    >>
    >> Steven Cheng
    >> Microsoft Online Support
    >>
    >> Get Secure! www.microsoft.com/security
    >> (This posting is provided "AS IS", with no warranties, and confers no
    >> rights.)
    >>
    >>
    >> --------------------
    >> | From: "Terry Holland" <>
    >> | Subject: Dynamically adding custom control
    >> | Date: Thu, 17 Nov 2005 21:28:24 -0000
    >> | Lines: 149
    >> | X-Priority: 3
    >> | X-MSMail-Priority: Normal
    >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    >> | X-RFC2646: Format=Flowed; Original
    >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    >> | Message-ID: <#>
    >> | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >> | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    >> | Xref: TK2MSFTNGXA02.phx.gbl
    >> microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >> |
    >> | Im trying to dynamically add a user control for a webform but it is not
    >> | appearing when I run the form.
    >> |
    >> | I have pasted the code that I am using. Can someone advise what Im
    >> doing
    >> | wrong. My user control (ctlTextBox) contains one textbox. When I
    >> dragged
    >> | this onto the control, the line
    >> |
    >> | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    >> |
    >> | was added the declarations area of the code module. Without changing
    >> this
    >> | to
    >> |
    >> | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    >> |
    >> | An error is generated on my page as soon as I hit the line
    >> |
    >> | .Text = "Tesing Control"
    >> |
    >> | Also, if I drag ctlTextBox onto my form at design time, it displays OK
    >> |
    >> |
    >> | ==========================
    >> | User Control Code
    >> | ==========================
    >> | Public Class ctlTextBox
    >> | Inherits System.Web.UI.UserControl
    >> |
    >> | #Region " Web Form Designer Generated Code "
    >> |
    >> | 'This call is required by the Web Form Designer.
    >> | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    >> | InitializeComponent()
    >> |
    >> | End Sub
    >> |
    >> | '############################################################
    >> | '## This is the code that was generated
    >> | ############
    >> | '## but without the New keyword, a runtime error is generated
    >> | ############
    >> | '## when I try to set the Text property on the page
    >> | ############
    >> | '############################################################
    >> | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    >> | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    >> |
    >> | 'NOTE: The following placeholder declaration is required by the Web
    >> Form
    >> | Designer.
    >> | 'Do not delete or move it.
    >> | Private designerPlaceholderDeclaration As System.Object
    >> |
    >> | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    >> | System.EventArgs) Handles MyBase.Init
    >> | 'CODEGEN: This method call is required by the Web Form Designer
    >> | 'Do not modify it using the code editor.
    >> | InitializeComponent()
    >> | End Sub
    >> |
    >> | #End Region
    >> |
    >> | Public Property Width() As Integer
    >> | Get
    >> | Return txt.Width.Value
    >> | End Get
    >> | Set(ByVal Value As Integer)
    >> | txt.Width = Unit.Pixel(Value)
    >> | End Set
    >> | End Property
    >> |
    >> | Public Property Text() As String
    >> | Get
    >> | Return txt.Text
    >> | End Get
    >> | Set(ByVal Value As String)
    >> | txt.Text = Value
    >> | End Set
    >> | End Property
    >> |
    >> | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> | System.EventArgs) Handles MyBase.Load
    >> | 'Put user code to initialize the page here
    >> | txt.CssClass = "Editable"
    >> | End Sub
    >> |
    >> | End Class
    >> | ===========================
    >> | End User Control Code
    >> | ===========================
    >> |
    >> |
    >> | ===========================
    >> | Page Code
    >> | ===========================
    >> | Public Class pgeTest
    >> | Inherits System.Web.UI.Page
    >> |
    >> | #Region " Web Form Designer Generated Code "
    >> |
    >> | 'This call is required by the Web Form Designer.
    >> | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    >> | InitializeComponent()
    >> |
    >> | End Sub
    >> |
    >> | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    >> |
    >> | 'NOTE: The following placeholder declaration is required by the Web
    >> Form
    >> | Designer.
    >> | 'Do not delete or move it.
    >> | Private designerPlaceholderDeclaration As System.Object
    >> |
    >> | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    >> | System.EventArgs) Handles MyBase.Init
    >> | 'CODEGEN: This method call is required by the Web Form Designer
    >> | 'Do not modify it using the code editor.
    >> | InitializeComponent()
    >> | End Sub
    >> |
    >> | #End Region
    >> |
    >> | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> | System.EventArgs) Handles MyBase.Load
    >> | BuildControls()
    >> | End Sub
    >> |
    >> |
    >> | Sub BuildControls()
    >> | Dim tb As New ctlTextBox
    >> | With tb
    >> | .Text = "Tesing Control"
    >> | End With
    >> | Panel1.Controls.Add(tb)
    >> |
    >> | Dim txt As New TextBox
    >> | txt.Text = "Hello"
    >> | Panel1.Controls.Add(txt)
    >> |
    >> | Dim lbl As New Label
    >> | lbl.Text = "Label"
    >> | Panel1.Controls.Add(lbl)
    >> |
    >> | End Sub
    >> |
    >> | End Class
    >> | ===============================
    >> | End of Page Code
    >> | ===============================
    >> |
    >> |
    >> |
    >>

    >
    >
     
    Terry Holland, Nov 22, 2005
    #6
  7. Hey Terry,

    As for custom webserver control(need to expose some nested sub control's
    properties...), we need to pay attention to serveral things especially when
    they're dynanically created on page. So I'd suggest you try making them
    work when you statically declare them on page, that'll help make your
    problem simplified...

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | From: "Terry Holland" <>
    | References: <#>
    <jlvfhG$>
    <>
    | Subject: Re: Dynamically adding custom control
    | Date: Tue, 22 Nov 2005 16:43:10 -0000
    | Lines: 273
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | X-RFC2646: Format=Flowed; Response
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: host96.multiserv.com 194.200.135.96
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:14032
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | Steven
    |
    | Im getting in a bit of a mess with this. What I want to end up with is a
    | set of controls for editing / displaying fields on a webform. I would
    like
    | to be able to dynamically add these controls to webforms as and when I
    need
    | them.
    |
    | Initially I need three types of fields
    |
    | ctlFieldReadOnly
    | ctlFieldEditable
    | ctlFieldEditableRequired
    |
    | Each of these controls will consist of the following controls:
    |
    | ctlFieldReadOnly: Label (lblLabel) and a label (lblValue) to display the
    | data
    | ctlFieldEditable: Label (lblLabel) and a textbox (txtValue - background
    | colour blue) to the display the data
    | ctlFieldEditableRequired: Label (lblLabel) and a textbox (txtValue -
    | background colour yellow) to the display the data.
    |
    | Each of these controls needs to expose a Label property and a Value
    | property.
    | Each control should be able to be configured so the either, the Label
    | control is above the Value control, or the Label control appears to the
    Left
    | of the Value control.
    |
    | Each form may contain any number of each type of control.
    |
    | Ive been trying to develop along the following lines:
    | Create a Base control such as ctlField that must be inherited. This
    control
    | will contain the properties Label, Value, Width and HorizontalAlignment.
    | Im then trying to derive the required controls from this control. My
    derived
    | controls should add the relevant webcontrols ie Label to display the
    Value
    | for a ctlReadOnly control, a Textbox to display the Value for
    | ctlFieldEditable & ctlFieldEditableRequired controls.
    | Im getting various results ranging from no controls being displayed, not
    | being able to refer to controls in order to read/write values.
    |
    | It would be very helpful if you could provide a small sample of how I
    might
    | get this sort of scenario to work (VB if possible - I know you prefer C#)
    |
    |
    | Terry
    |
    |
    |
    | "Terry Holland" <> wrote in message
    | news:...
    | > We meet again Steven.
    | >
    | > Ive modified my code to remove the New keyword and added the
    LoadControl
    | > method in my page to load the control and now the user control appears
    on
    | > the screen.
    | >
    | > Thanks
    | >
    | > Terry Holland
    | >
    | >
    | > "Steven Cheng[MSFT]" <> wrote in message
    | > news:jlvfhG$...
    | >> Hi Terry,
    | >>
    | >> As for ASP.NET Usercontrol(ascx...), the Control instances are all
    | >> defined
    | >> in the .ascx template file(if not dynamically adding control...), so
    we
    | >> should not declare "New" for control reference like:
    | >>
    | >> Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | >>
    | >> If you get error when not using "New", there must have something
    | >> incorrect
    | >> in our page or code logic. Would you also provide the ascx template
    | >> together with the code behind?
    | >>
    | >> In addition, for dynamically adding Usercontrol onto aspx page, we
    should
    | >> use the Page.LoadControl method to load the "xxx.ascx" so as to create
    | >> the
    | >> UserControl instance and add it into Container control in each Page
    | >> Request....
    | >>
    | >> If there're anything unclear, please feel free to post here.
    | >>
    | >> Regards,
    | >>
    | >> Steven Cheng
    | >> Microsoft Online Support
    | >>
    | >> Get Secure! www.microsoft.com/security
    | >> (This posting is provided "AS IS", with no warranties, and confers no
    | >> rights.)
    | >>
    | >>
    | >> --------------------
    | >> | From: "Terry Holland" <>
    | >> | Subject: Dynamically adding custom control
    | >> | Date: Thu, 17 Nov 2005 21:28:24 -0000
    | >> | Lines: 149
    | >> | X-Priority: 3
    | >> | X-MSMail-Priority: Normal
    | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | >> | X-RFC2646: Format=Flowed; Original
    | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | >> | Message-ID: <#>
    | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | >> | NNTP-Posting-Host: host10.harsco.com 209.183.189.10
    | >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | >> | Xref: TK2MSFTNGXA02.phx.gbl
    | >> microsoft.public.dotnet.framework.aspnet.buildingcontrols:13997
    | >> | X-Tomcat-NG:
    microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | >> |
    | >> | Im trying to dynamically add a user control for a webform but it is
    not
    | >> | appearing when I run the form.
    | >> |
    | >> | I have pasted the code that I am using. Can someone advise what Im
    | >> doing
    | >> | wrong. My user control (ctlTextBox) contains one textbox. When I
    | >> dragged
    | >> | this onto the control, the line
    | >> |
    | >> | Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    | >> |
    | >> | was added the declarations area of the code module. Without changing
    | >> this
    | >> | to
    | >> |
    | >> | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | >> |
    | >> | An error is generated on my page as soon as I hit the line
    | >> |
    | >> | .Text = "Tesing Control"
    | >> |
    | >> | Also, if I drag ctlTextBox onto my form at design time, it displays
    OK
    | >> |
    | >> |
    | >> | ==========================
    | >> | User Control Code
    | >> | ==========================
    | >> | Public Class ctlTextBox
    | >> | Inherits System.Web.UI.UserControl
    | >> |
    | >> | #Region " Web Form Designer Generated Code "
    | >> |
    | >> | 'This call is required by the Web Form Designer.
    | >> | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | >> | InitializeComponent()
    | >> |
    | >> | End Sub
    | >> |
    | >> | '############################################################
    | >> | '## This is the code that was generated
    | >> | ############
    | >> | '## but without the New keyword, a runtime error is generated
    | >> | ############
    | >> | '## when I try to set the Text property on the page
    | >> | ############
    | >> | '############################################################
    | >> | 'Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    | >> | Protected WithEvents txt As New System.Web.UI.WebControls.TextBox
    | >> |
    | >> | 'NOTE: The following placeholder declaration is required by the
    Web
    | >> Form
    | >> | Designer.
    | >> | 'Do not delete or move it.
    | >> | Private designerPlaceholderDeclaration As System.Object
    | >> |
    | >> | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | >> | System.EventArgs) Handles MyBase.Init
    | >> | 'CODEGEN: This method call is required by the Web Form
    Designer
    | >> | 'Do not modify it using the code editor.
    | >> | InitializeComponent()
    | >> | End Sub
    | >> |
    | >> | #End Region
    | >> |
    | >> | Public Property Width() As Integer
    | >> | Get
    | >> | Return txt.Width.Value
    | >> | End Get
    | >> | Set(ByVal Value As Integer)
    | >> | txt.Width = Unit.Pixel(Value)
    | >> | End Set
    | >> | End Property
    | >> |
    | >> | Public Property Text() As String
    | >> | Get
    | >> | Return txt.Text
    | >> | End Get
    | >> | Set(ByVal Value As String)
    | >> | txt.Text = Value
    | >> | End Set
    | >> | End Property
    | >> |
    | >> | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | >> | System.EventArgs) Handles MyBase.Load
    | >> | 'Put user code to initialize the page here
    | >> | txt.CssClass = "Editable"
    | >> | End Sub
    | >> |
    | >> | End Class
    | >> | ===========================
    | >> | End User Control Code
    | >> | ===========================
    | >> |
    | >> |
    | >> | ===========================
    | >> | Page Code
    | >> | ===========================
    | >> | Public Class pgeTest
    | >> | Inherits System.Web.UI.Page
    | >> |
    | >> | #Region " Web Form Designer Generated Code "
    | >> |
    | >> | 'This call is required by the Web Form Designer.
    | >> | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | >> | InitializeComponent()
    | >> |
    | >> | End Sub
    | >> |
    | >> | Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    | >> |
    | >> | 'NOTE: The following placeholder declaration is required by the
    Web
    | >> Form
    | >> | Designer.
    | >> | 'Do not delete or move it.
    | >> | Private designerPlaceholderDeclaration As System.Object
    | >> |
    | >> | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | >> | System.EventArgs) Handles MyBase.Init
    | >> | 'CODEGEN: This method call is required by the Web Form
    Designer
    | >> | 'Do not modify it using the code editor.
    | >> | InitializeComponent()
    | >> | End Sub
    | >> |
    | >> | #End Region
    | >> |
    | >> | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | >> | System.EventArgs) Handles MyBase.Load
    | >> | BuildControls()
    | >> | End Sub
    | >> |
    | >> |
    | >> | Sub BuildControls()
    | >> | Dim tb As New ctlTextBox
    | >> | With tb
    | >> | .Text = "Tesing Control"
    | >> | End With
    | >> | Panel1.Controls.Add(tb)
    | >> |
    | >> | Dim txt As New TextBox
    | >> | txt.Text = "Hello"
    | >> | Panel1.Controls.Add(txt)
    | >> |
    | >> | Dim lbl As New Label
    | >> | lbl.Text = "Label"
    | >> | Panel1.Controls.Add(lbl)
    | >> |
    | >> | End Sub
    | >> |
    | >> | End Class
    | >> | ===============================
    | >> | End of Page Code
    | >> | ===============================
    | >> |
    | >> |
    | >> |
    | >>
    | >
    | >
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 23, 2005
    #7
  8. Ok. This is what I am trying.

    I have a base control called ctlField. From this I derive another few
    controls, one of which is ctlFieldHEditableRequired.

    I am placing a ctlFieldHEditableRequired and a Button onto my page
    WebForm1.aspx.

    When I click my button I want to use the values from my
    ctlFieldHEditableRequired control to save to a database. However, when I
    click my button, the values that I retrieve from my user control do not
    reflect any changes that were made.
    I have pasted enough of a a sample of my code to demonstrate the problems
    that Im having. If I run my project the values in my control are
    Label: New Label
    Textbox: New Value

    If I change the value in the Textbox to XXX and click the button on the
    page, the value that gets written to the page is New Value instead of XXX.
    How do I overcome this problem?


    BASE CONTROL HTML -ctlField
    =========================
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="ctlField.ascx.vb" Inherits="UserControl.ctlField"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

    BASE CONTROL CODE BEHIND
    =========================
    Public Class ctlField
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region
    Protected m_intLabelWidth As Integer = 100
    Protected m_intValueWidth As Integer = 150
    Protected m_strLabel As String = "Label"
    Protected m_strValue As String = "Value"


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub


    Public Property LabelWidth() As Integer
    Get
    Return m_intLabelWidth
    End Get
    Set(ByVal Value As Integer)
    m_intLabelWidth = Value
    End Set
    End Property

    Public Property ValueWidth() As Integer
    Get
    Return m_intValueWidth
    End Get
    Set(ByVal Value As Integer)
    m_intValueWidth = Value
    End Set
    End Property

    Public Property Label() As String
    Get
    Return m_strLabel
    End Get
    Set(ByVal Value As String)
    m_strLabel = Value
    End Set
    End Property

    Public Property Value() As String
    Get
    Return m_strValue
    End Get
    Set(ByVal Value As String)
    m_strValue = Value
    End Set
    End Property
    End Class


    DERIVED CONTROL HTML - ctlFieldHEditableRequired
    =========================================
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="ctlFieldHEditableRequired.ascx.vb"
    Inherits="UserControl.ctlFieldHEditableRequired"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:label id="Label1" Width="496px"
    runat="server">Label</asp:label><asp:textbox id="TextBox1" Width="16px"
    runat="server"></asp:textbox>

    DERIVED CONTROL CODE BEHIND
    =============================
    Public Class ctlFieldHEditableRequired
    Inherits ctlField ' System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If IsPostBack Then Exit Sub

    With Label1
    .Width = Unit.Pixel(m_intLabelWidth)
    .Text = m_strLabel
    End With

    With TextBox1
    .Width = Unit.Pixel(m_intValueWidth)
    .Text = m_strValue
    .CssClass = "FieldEditableRequired"
    End With

    End Sub

    End Class


    WebForm1 HTML
    ==============
    <%@ Register TagPrefix="uc1" TagName="ctlFieldHEditableRequired"
    Src="ctlFieldHEditableRequired.ascx" %>
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
    Inherits="UserControl.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <P>
    <uc1:ctlFieldHEditableRequired id="ctlFieldHEditableRequired1"
    runat="server"></uc1:ctlFieldHEditableRequired></P>
    <P>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
    </form>
    </body>
    </HTML>


    WebForm1 CODEBEHIND
    ===================
    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Protected WithEvents ctlFieldHEditableRequired1 As
    ctlFieldHEditableRequired
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    InitialiseControls()

    If Not IsPostBack Then
    GetProperties()
    End If
    End Sub

    Private Sub GetProperties()

    With ctlFieldHEditableRequired1
    .Label = "New Label"
    .Value = "New Value"
    End With

    End Sub
    Private Sub InitialiseControls()

    With ctlFieldHEditableRequired1
    .LabelWidth = 300
    .ValueWidth = 300
    .Label = "ID"
    End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    Response.Write(ctlFieldHEditableRequired1.Value)
    End Sub
    End Class
     
    Terry Holland, Nov 25, 2005
    #8
  9. Steven

    I just realised Im using wrong terminology. Im not creating "Custom
    Controls", I'm creating "User Controls".

    Apologies for any confusion

    Terry
     
    Terry Holland, Nov 26, 2005
    #9
  10. Thanks for your response Terry,

    I've have a look and perfom some tests on them and update you soon.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | From: "Terry Holland" <>
    | References: <#>
    <jlvfhG$>
    <>
    <>
    <>
    | Subject: Re: Dynamically adding custom control
    | Date: Fri, 25 Nov 2005 18:57:20 -0000
    | Lines: 258
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: 82.152.27.185
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:14046
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | Ok. This is what I am trying.
    |
    | I have a base control called ctlField. From this I derive another few
    | controls, one of which is ctlFieldHEditableRequired.
    |
    | I am placing a ctlFieldHEditableRequired and a Button onto my page
    | WebForm1.aspx.
    |
    | When I click my button I want to use the values from my
    | ctlFieldHEditableRequired control to save to a database. However, when I
    | click my button, the values that I retrieve from my user control do not
    | reflect any changes that were made.
    | I have pasted enough of a a sample of my code to demonstrate the problems
    | that Im having. If I run my project the values in my control are
    | Label: New Label
    | Textbox: New Value
    |
    | If I change the value in the Textbox to XXX and click the button on the
    | page, the value that gets written to the page is New Value instead of
    XXX.
    | How do I overcome this problem?
    |
    |
    | BASE CONTROL HTML -ctlField
    | =========================
    | <%@ Control Language="vb" AutoEventWireup="false"
    | Codebehind="ctlField.ascx.vb" Inherits="UserControl.ctlField"
    | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    |
    | BASE CONTROL CODE BEHIND
    | =========================
    | Public Class ctlField
    | Inherits System.Web.UI.UserControl
    |
    | #Region " Web Form Designer Generated Code "
    |
    | 'This call is required by the Web Form Designer.
    | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | InitializeComponent()
    |
    | End Sub
    |
    | 'NOTE: The following placeholder declaration is required by the Web
    Form
    | Designer.
    | 'Do not delete or move it.
    | Private designerPlaceholderDeclaration As System.Object
    |
    | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Init
    | 'CODEGEN: This method call is required by the Web Form Designer
    | 'Do not modify it using the code editor.
    | InitializeComponent()
    | End Sub
    |
    | #End Region
    | Protected m_intLabelWidth As Integer = 100
    | Protected m_intValueWidth As Integer = 150
    | Protected m_strLabel As String = "Label"
    | Protected m_strValue As String = "Value"
    |
    |
    | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    | 'Put user code to initialize the page here
    | End Sub
    |
    |
    | Public Property LabelWidth() As Integer
    | Get
    | Return m_intLabelWidth
    | End Get
    | Set(ByVal Value As Integer)
    | m_intLabelWidth = Value
    | End Set
    | End Property
    |
    | Public Property ValueWidth() As Integer
    | Get
    | Return m_intValueWidth
    | End Get
    | Set(ByVal Value As Integer)
    | m_intValueWidth = Value
    | End Set
    | End Property
    |
    | Public Property Label() As String
    | Get
    | Return m_strLabel
    | End Get
    | Set(ByVal Value As String)
    | m_strLabel = Value
    | End Set
    | End Property
    |
    | Public Property Value() As String
    | Get
    | Return m_strValue
    | End Get
    | Set(ByVal Value As String)
    | m_strValue = Value
    | End Set
    | End Property
    | End Class
    |
    |
    | DERIVED CONTROL HTML - ctlFieldHEditableRequired
    | =========================================
    | <%@ Control Language="vb" AutoEventWireup="false"
    | Codebehind="ctlFieldHEditableRequired.ascx.vb"
    | Inherits="UserControl.ctlFieldHEditableRequired"
    | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    | <asp:label id="Label1" Width="496px"
    | runat="server">Label</asp:label><asp:textbox id="TextBox1" Width="16px"
    | runat="server"></asp:textbox>
    |
    | DERIVED CONTROL CODE BEHIND
    | =============================
    | Public Class ctlFieldHEditableRequired
    | Inherits ctlField ' System.Web.UI.UserControl
    |
    | #Region " Web Form Designer Generated Code "
    |
    | 'This call is required by the Web Form Designer.
    | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | InitializeComponent()
    |
    | End Sub
    |
    | Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    | Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    |
    | 'NOTE: The following placeholder declaration is required by the Web
    Form
    | Designer.
    | 'Do not delete or move it.
    | Private designerPlaceholderDeclaration As System.Object
    |
    | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Init
    | 'CODEGEN: This method call is required by the Web Form Designer
    | 'Do not modify it using the code editor.
    | InitializeComponent()
    | End Sub
    |
    | #End Region
    |
    | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    | 'Put user code to initialize the page here
    | If IsPostBack Then Exit Sub
    |
    | With Label1
    | .Width = Unit.Pixel(m_intLabelWidth)
    | .Text = m_strLabel
    | End With
    |
    | With TextBox1
    | .Width = Unit.Pixel(m_intValueWidth)
    | .Text = m_strValue
    | .CssClass = "FieldEditableRequired"
    | End With
    |
    | End Sub
    |
    | End Class
    |
    |
    | WebForm1 HTML
    | ==============
    | <%@ Register TagPrefix="uc1" TagName="ctlFieldHEditableRequired"
    | Src="ctlFieldHEditableRequired.ascx" %>
    | <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb"
    | Inherits="UserControl.WebForm1"%>
    | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    | <HTML>
    | <HEAD>
    | <title>WebForm1</title>
    | <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    | <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    | <meta name="vs_defaultClientScript" content="JavaScript">
    | <meta name="vs_targetSchema"
    | content="http://schemas.microsoft.com/intellisense/ie5">
    | </HEAD>
    | <body>
    | <form id="Form1" method="post" runat="server">
    | <P>
    | <uc1:ctlFieldHEditableRequired id="ctlFieldHEditableRequired1"
    | runat="server"></uc1:ctlFieldHEditableRequired></P>
    | <P>
    | <asp:Button id="Button1" runat="server"
    Text="Button"></asp:Button></P>
    | </form>
    | </body>
    | </HTML>
    |
    |
    | WebForm1 CODEBEHIND
    | ===================
    | Public Class WebForm1
    | Inherits System.Web.UI.Page
    |
    | #Region " Web Form Designer Generated Code "
    |
    | 'This call is required by the Web Form Designer.
    | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | InitializeComponent()
    |
    | End Sub
    |
    | Protected WithEvents ctlFieldHEditableRequired1 As
    | ctlFieldHEditableRequired
    | Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    |
    | 'NOTE: The following placeholder declaration is required by the Web
    Form
    | Designer.
    | 'Do not delete or move it.
    | Private designerPlaceholderDeclaration As System.Object
    |
    | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles MyBase.Init
    | 'CODEGEN: This method call is required by the Web Form Designer
    | 'Do not modify it using the code editor.
    | InitializeComponent()
    | End Sub
    |
    | #End Region
    |
    | Private Sub Page_Load(ByVal sender As Object, ByVal e As
    | System.EventArgs) Handles MyBase.Load
    |
    | InitialiseControls()
    |
    | If Not IsPostBack Then
    | GetProperties()
    | End If
    | End Sub
    |
    | Private Sub GetProperties()
    |
    | With ctlFieldHEditableRequired1
    | .Label = "New Label"
    | .Value = "New Value"
    | End With
    |
    | End Sub
    | Private Sub InitialiseControls()
    |
    | With ctlFieldHEditableRequired1
    | .LabelWidth = 300
    | .ValueWidth = 300
    | .Label = "ID"
    | End With
    | End Sub
    |
    | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    | System.EventArgs) Handles Button1.Click
    | Response.Write(ctlFieldHEditableRequired1.Value)
    | End Sub
    | End Class
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 28, 2005
    #10
  11. Thanks Steven

    I had started going along the path of creating an interface that each of my
    controls would implement but have decided to go with your method.
    Thanks for your input.

    In your code you have the line in each of your properties

    Me.EnsureChildControls()

    Could you explain this for me please as I dont fully understand the helpfile





    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Terry,
    >
    > I think there're serveral things you can have a look in your controls:
    >
    > 1. As for those Propertys you want to define in base class and implement
    > concrete defination in sub classes, you can declare them as "Overridable
    > Property" in base UserControl class, then in your sub UserControl, just
    > override them.
    >
    >
    > 2. Also, since you'd like to associate those propetey with your

    Controls(in
    > the sub Usercontrol)'s properties, you need to implement those code in the
    > overrides properties.
    >
    > To make the above things clear, I've built my own version of the
    > usercontrols and test pages, I've attached the files in this message, you
    > can download them and have a test on your side.
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    > --------------------
    > | X-Tomcat-ID: 121362098
    > | References: <#>
    > <jlvfhG$>
    > <>
    > <>
    > <>
    > <#>
    > | MIME-Version: 1.0
    > | Content-Type: text/plain
    > | Content-Transfer-Encoding: 7bit
    > | From: (Steven Cheng[MSFT])
    > | Organization: Microsoft
    > | Date: Mon, 28 Nov 2005 07:55:10 GMT
    > | Subject: Re: Dynamically adding custom control
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | Message-ID: <JvllTE$>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | Lines: 242
    > | Path: TK2MSFTNGXA02.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.buildingcontrols:14052
    > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
    > |
    > | Thanks for your response Terry,
    > |
    > | I've have a look and perfom some tests on them and update you soon.
    > |
    > | Regards,
    > |
    > | Steven Cheng
    > | Microsoft Online Support
    > |
    > | Get Secure! www.microsoft.com/security
    > | (This posting is provided "AS IS", with no warranties, and confers no
    > | rights.)
    > | --------------------
    > | | From: "Terry Holland" <>
    > | | References: <#>
    > | <jlvfhG$>
    > | <>
    > | <>
    > | <>
    > | | Subject: Re: Dynamically adding custom control
    > | | Date: Fri, 25 Nov 2005 18:57:20 -0000
    > | | Lines: 258
    > | | X-Priority: 3
    > | | X-MSMail-Priority: Normal
    > | | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | | X-RFC2646: Format=Flowed; Original
    > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | | Message-ID: <#>
    > | | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | | NNTP-Posting-Host: 82.152.27.185
    > | | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    > | | Xref: TK2MSFTNGXA02.phx.gbl
    > | microsoft.public.dotnet.framework.aspnet.buildingcontrols:14046
    > | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > | |
    > | | Ok. This is what I am trying.
    > | |
    > | | I have a base control called ctlField. From this I derive another few
    > | | controls, one of which is ctlFieldHEditableRequired.
    > | |
    > | | I am placing a ctlFieldHEditableRequired and a Button onto my page
    > | | WebForm1.aspx.
    > | |
    > | | When I click my button I want to use the values from my
    > | | ctlFieldHEditableRequired control to save to a database. However, when
    > I
    > | | click my button, the values that I retrieve from my user control do

    not
    > | | reflect any changes that were made.
    > | | I have pasted enough of a a sample of my code to demonstrate the
    > problems
    > | | that Im having. If I run my project the values in my control are
    > | | Label: New Label
    > | | Textbox: New Value
    > | |
    > | | If I change the value in the Textbox to XXX and click the button on

    the
    > | | page, the value that gets written to the page is New Value instead of
    > | XXX.
    > | | How do I overcome this problem?
    > | |
    > | |
    > | | BASE CONTROL HTML -ctlField
    > | | =========================
    > | | <%@ Control Language="vb" AutoEventWireup="false"
    > | | Codebehind="ctlField.ascx.vb" Inherits="UserControl.ctlField"
    > | | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > | |
    > | | BASE CONTROL CODE BEHIND
    > | | =========================
    > | | Public Class ctlField
    > | | Inherits System.Web.UI.UserControl
    > | |
    > | | #Region " Web Form Designer Generated Code "
    > | |
    > | | 'This call is required by the Web Form Designer.
    > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | | InitializeComponent()
    > | |
    > | | End Sub
    > | |
    > | | 'NOTE: The following placeholder declaration is required by the

    Web
    > | Form
    > | | Designer.
    > | | 'Do not delete or move it.
    > | | Private designerPlaceholderDeclaration As System.Object
    > | |
    > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Init
    > | | 'CODEGEN: This method call is required by the Web Form

    Designer
    > | | 'Do not modify it using the code editor.
    > | | InitializeComponent()
    > | | End Sub
    > | |
    > | | #End Region
    > | | Protected m_intLabelWidth As Integer = 100
    > | | Protected m_intValueWidth As Integer = 150
    > | | Protected m_strLabel As String = "Label"
    > | | Protected m_strValue As String = "Value"
    > | |
    > | |
    > | | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Load
    > | | 'Put user code to initialize the page here
    > | | End Sub
    > | |
    > | |
    > | | Public Property LabelWidth() As Integer
    > | | Get
    > | | Return m_intLabelWidth
    > | | End Get
    > | | Set(ByVal Value As Integer)
    > | | m_intLabelWidth = Value
    > | | End Set
    > | | End Property
    > | |
    > | | Public Property ValueWidth() As Integer
    > | | Get
    > | | Return m_intValueWidth
    > | | End Get
    > | | Set(ByVal Value As Integer)
    > | | m_intValueWidth = Value
    > | | End Set
    > | | End Property
    > | |
    > | | Public Property Label() As String
    > | | Get
    > | | Return m_strLabel
    > | | End Get
    > | | Set(ByVal Value As String)
    > | | m_strLabel = Value
    > | | End Set
    > | | End Property
    > | |
    > | | Public Property Value() As String
    > | | Get
    > | | Return m_strValue
    > | | End Get
    > | | Set(ByVal Value As String)
    > | | m_strValue = Value
    > | | End Set
    > | | End Property
    > | | End Class
    > | |
    > | |
    > | | DERIVED CONTROL HTML - ctlFieldHEditableRequired
    > | | =========================================
    > | | <%@ Control Language="vb" AutoEventWireup="false"
    > | | Codebehind="ctlFieldHEditableRequired.ascx.vb"
    > | | Inherits="UserControl.ctlFieldHEditableRequired"
    > | | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > | | <asp:label id="Label1" Width="496px"
    > | | runat="server">Label</asp:label><asp:textbox id="TextBox1"

    Width="16px"
    > | | runat="server"></asp:textbox>
    > | |
    > | | DERIVED CONTROL CODE BEHIND
    > | | =============================
    > | | Public Class ctlFieldHEditableRequired
    > | | Inherits ctlField ' System.Web.UI.UserControl
    > | |
    > | | #Region " Web Form Designer Generated Code "
    > | |
    > | | 'This call is required by the Web Form Designer.
    > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | | InitializeComponent()
    > | |
    > | | End Sub
    > | |
    > | | Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    > | | Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    > | |
    > | | 'NOTE: The following placeholder declaration is required by the

    Web
    > | Form
    > | | Designer.
    > | | 'Do not delete or move it.
    > | | Private designerPlaceholderDeclaration As System.Object
    > | |
    > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Init
    > | | 'CODEGEN: This method call is required by the Web Form

    Designer
    > | | 'Do not modify it using the code editor.
    > | | InitializeComponent()
    > | | End Sub
    > | |
    > | | #End Region
    > | |
    > | | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Load
    > | | 'Put user code to initialize the page here
    > | | If IsPostBack Then Exit Sub
    > | |
    > | | With Label1
    > | | .Width = Unit.Pixel(m_intLabelWidth)
    > | | .Text = m_strLabel
    > | | End With
    > | |
    > | | With TextBox1
    > | | .Width = Unit.Pixel(m_intValueWidth)
    > | | .Text = m_strValue
    > | | .CssClass = "FieldEditableRequired"
    > | | End With
    > | |
    > | | End Sub
    > | |
    > | | End Class
    > | |
    > | |
    > | | WebForm1 HTML
    > | | ==============
    > | | <%@ Register TagPrefix="uc1" TagName="ctlFieldHEditableRequired"
    > | | Src="ctlFieldHEditableRequired.ascx" %>
    > | | <%@ Page Language="vb" AutoEventWireup="false"
    > | Codebehind="WebForm1.aspx.vb"
    > | | Inherits="UserControl.WebForm1"%>
    > | | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > | | <HTML>
    > | | <HEAD>
    > | | <title>WebForm1</title>
    > | | <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > | | <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > | | <meta name="vs_defaultClientScript" content="JavaScript">
    > | | <meta name="vs_targetSchema"
    > | | content="http://schemas.microsoft.com/intellisense/ie5">
    > | | </HEAD>
    > | | <body>
    > | | <form id="Form1" method="post" runat="server">
    > | | <P>
    > | | <uc1:ctlFieldHEditableRequired id="ctlFieldHEditableRequired1"
    > | | runat="server"></uc1:ctlFieldHEditableRequired></P>
    > | | <P>
    > | | <asp:Button id="Button1" runat="server"
    > | Text="Button"></asp:Button></P>
    > | | </form>
    > | | </body>
    > | | </HTML>
    > | |
    > | |
    > | | WebForm1 CODEBEHIND
    > | | ===================
    > | | Public Class WebForm1
    > | | Inherits System.Web.UI.Page
    > | |
    > | | #Region " Web Form Designer Generated Code "
    > | |
    > | | 'This call is required by the Web Form Designer.
    > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > | | InitializeComponent()
    > | |
    > | | End Sub
    > | |
    > | | Protected WithEvents ctlFieldHEditableRequired1 As
    > | | ctlFieldHEditableRequired
    > | | Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    > | |
    > | | 'NOTE: The following placeholder declaration is required by the

    Web
    > | Form
    > | | Designer.
    > | | 'Do not delete or move it.
    > | | Private designerPlaceholderDeclaration As System.Object
    > | |
    > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Init
    > | | 'CODEGEN: This method call is required by the Web Form

    Designer
    > | | 'Do not modify it using the code editor.
    > | | InitializeComponent()
    > | | End Sub
    > | |
    > | | #End Region
    > | |
    > | | Private Sub Page_Load(ByVal sender As Object, ByVal e As
    > | | System.EventArgs) Handles MyBase.Load
    > | |
    > | | InitialiseControls()
    > | |
    > | | If Not IsPostBack Then
    > | | GetProperties()
    > | | End If
    > | | End Sub
    > | |
    > | | Private Sub GetProperties()
    > | |
    > | | With ctlFieldHEditableRequired1
    > | | .Label = "New Label"
    > | | .Value = "New Value"
    > | | End With
    > | |
    > | | End Sub
    > | | Private Sub InitialiseControls()
    > | |
    > | | With ctlFieldHEditableRequired1
    > | | .LabelWidth = 300
    > | | .ValueWidth = 300
    > | | .Label = "ID"
    > | | End With
    > | | End Sub
    > | |
    > | | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e

    As
    > | | System.EventArgs) Handles Button1.Click
    > | | Response.Write(ctlFieldHEditableRequired1.Value)
    > | | End Sub
    > | | End Class
    > | |
    > | |
    > | |
    > |
    > |
     
    Terry Holland, Nov 29, 2005
    #11
  12. Thanks for your reply Terry,

    EnsureChildControls method is just make sure the control hierarchy of the
    Control has been constructed, if not, it'll call CreateChildControls to
    construct the control.... Sometimes we expose child control's Property as
    our composite control's property, we need to add this to ensure child
    control is available when accessing the property.....

    #Control.EnsureChildControls Method
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemwebuicontrolclassensurechildcontrolstopic.asp

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    --------------------
    | From: "Terry Holland" <>
    | References: <#>
    <jlvfhG$>
    <>
    <>
    <>
    <#>
    <JvllTE$>
    <>
    | Subject: Re: Dynamically adding custom control
    | Date: Tue, 29 Nov 2005 10:55:17 -0000
    | Lines: 387
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    | Message-ID: <#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: host240.multiserv.com 194.200.135.240
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:14058
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | Thanks Steven
    |
    | I had started going along the path of creating an interface that each of
    my
    | controls would implement but have decided to go with your method.
    | Thanks for your input.
    |
    | In your code you have the line in each of your properties
    |
    | Me.EnsureChildControls()
    |
    | Could you explain this for me please as I dont fully understand the
    helpfile
    |
    |
    |
    |
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:...
    | > Hi Terry,
    | >
    | > I think there're serveral things you can have a look in your controls:
    | >
    | > 1. As for those Propertys you want to define in base class and implement
    | > concrete defination in sub classes, you can declare them as "Overridable
    | > Property" in base UserControl class, then in your sub UserControl, just
    | > override them.
    | >
    | >
    | > 2. Also, since you'd like to associate those propetey with your
    | Controls(in
    | > the sub Usercontrol)'s properties, you need to implement those code in
    the
    | > overrides properties.
    | >
    | > To make the above things clear, I've built my own version of the
    | > usercontrols and test pages, I've attached the files in this message,
    you
    | > can download them and have a test on your side.
    | >
    | > Hope helps. Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | >
    | > --------------------
    | > | X-Tomcat-ID: 121362098
    | > | References: <#>
    | > <jlvfhG$>
    | > <>
    | > <>
    | > <>
    | > <#>
    | > | MIME-Version: 1.0
    | > | Content-Type: text/plain
    | > | Content-Transfer-Encoding: 7bit
    | > | From: (Steven Cheng[MSFT])
    | > | Organization: Microsoft
    | > | Date: Mon, 28 Nov 2005 07:55:10 GMT
    | > | Subject: Re: Dynamically adding custom control
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > | Message-ID: <JvllTE$>
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > | Lines: 242
    | > | Path: TK2MSFTNGXA02.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.buildingcontrols:14052
    | > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
    | > |
    | > | Thanks for your response Terry,
    | > |
    | > | I've have a look and perfom some tests on them and update you soon.
    | > |
    | > | Regards,
    | > |
    | > | Steven Cheng
    | > | Microsoft Online Support
    | > |
    | > | Get Secure! www.microsoft.com/security
    | > | (This posting is provided "AS IS", with no warranties, and confers no
    | > | rights.)
    | > | --------------------
    | > | | From: "Terry Holland" <>
    | > | | References: <#>
    | > | <jlvfhG$>
    | > | <>
    | > | <>
    | > | <>
    | > | | Subject: Re: Dynamically adding custom control
    | > | | Date: Fri, 25 Nov 2005 18:57:20 -0000
    | > | | Lines: 258
    | > | | X-Priority: 3
    | > | | X-MSMail-Priority: Normal
    | > | | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | > | | X-RFC2646: Format=Flowed; Original
    | > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | > | | Message-ID: <#>
    | > | | Newsgroups:
    microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > | | NNTP-Posting-Host: 82.152.27.185
    | > | | Path:
    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    | > | | Xref: TK2MSFTNGXA02.phx.gbl
    | > | microsoft.public.dotnet.framework.aspnet.buildingcontrols:14046
    | > | | X-Tomcat-NG:
    microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | > | |
    | > | | Ok. This is what I am trying.
    | > | |
    | > | | I have a base control called ctlField. From this I derive another
    few
    | > | | controls, one of which is ctlFieldHEditableRequired.
    | > | |
    | > | | I am placing a ctlFieldHEditableRequired and a Button onto my page
    | > | | WebForm1.aspx.
    | > | |
    | > | | When I click my button I want to use the values from my
    | > | | ctlFieldHEditableRequired control to save to a database. However,
    when
    | > I
    | > | | click my button, the values that I retrieve from my user control do
    | not
    | > | | reflect any changes that were made.
    | > | | I have pasted enough of a a sample of my code to demonstrate the
    | > problems
    | > | | that Im having. If I run my project the values in my control are
    | > | | Label: New Label
    | > | | Textbox: New Value
    | > | |
    | > | | If I change the value in the Textbox to XXX and click the button on
    | the
    | > | | page, the value that gets written to the page is New Value instead
    of
    | > | XXX.
    | > | | How do I overcome this problem?
    | > | |
    | > | |
    | > | | BASE CONTROL HTML -ctlField
    | > | | =========================
    | > | | <%@ Control Language="vb" AutoEventWireup="false"
    | > | | Codebehind="ctlField.ascx.vb" Inherits="UserControl.ctlField"
    | > | | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    | > | |
    | > | | BASE CONTROL CODE BEHIND
    | > | | =========================
    | > | | Public Class ctlField
    | > | | Inherits System.Web.UI.UserControl
    | > | |
    | > | | #Region " Web Form Designer Generated Code "
    | > | |
    | > | | 'This call is required by the Web Form Designer.
    | > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | > | | InitializeComponent()
    | > | |
    | > | | End Sub
    | > | |
    | > | | 'NOTE: The following placeholder declaration is required by the
    | Web
    | > | Form
    | > | | Designer.
    | > | | 'Do not delete or move it.
    | > | | Private designerPlaceholderDeclaration As System.Object
    | > | |
    | > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Init
    | > | | 'CODEGEN: This method call is required by the Web Form
    | Designer
    | > | | 'Do not modify it using the code editor.
    | > | | InitializeComponent()
    | > | | End Sub
    | > | |
    | > | | #End Region
    | > | | Protected m_intLabelWidth As Integer = 100
    | > | | Protected m_intValueWidth As Integer = 150
    | > | | Protected m_strLabel As String = "Label"
    | > | | Protected m_strValue As String = "Value"
    | > | |
    | > | |
    | > | | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Load
    | > | | 'Put user code to initialize the page here
    | > | | End Sub
    | > | |
    | > | |
    | > | | Public Property LabelWidth() As Integer
    | > | | Get
    | > | | Return m_intLabelWidth
    | > | | End Get
    | > | | Set(ByVal Value As Integer)
    | > | | m_intLabelWidth = Value
    | > | | End Set
    | > | | End Property
    | > | |
    | > | | Public Property ValueWidth() As Integer
    | > | | Get
    | > | | Return m_intValueWidth
    | > | | End Get
    | > | | Set(ByVal Value As Integer)
    | > | | m_intValueWidth = Value
    | > | | End Set
    | > | | End Property
    | > | |
    | > | | Public Property Label() As String
    | > | | Get
    | > | | Return m_strLabel
    | > | | End Get
    | > | | Set(ByVal Value As String)
    | > | | m_strLabel = Value
    | > | | End Set
    | > | | End Property
    | > | |
    | > | | Public Property Value() As String
    | > | | Get
    | > | | Return m_strValue
    | > | | End Get
    | > | | Set(ByVal Value As String)
    | > | | m_strValue = Value
    | > | | End Set
    | > | | End Property
    | > | | End Class
    | > | |
    | > | |
    | > | | DERIVED CONTROL HTML - ctlFieldHEditableRequired
    | > | | =========================================
    | > | | <%@ Control Language="vb" AutoEventWireup="false"
    | > | | Codebehind="ctlFieldHEditableRequired.ascx.vb"
    | > | | Inherits="UserControl.ctlFieldHEditableRequired"
    | > | | TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    | > | | <asp:label id="Label1" Width="496px"
    | > | | runat="server">Label</asp:label><asp:textbox id="TextBox1"
    | Width="16px"
    | > | | runat="server"></asp:textbox>
    | > | |
    | > | | DERIVED CONTROL CODE BEHIND
    | > | | =============================
    | > | | Public Class ctlFieldHEditableRequired
    | > | | Inherits ctlField ' System.Web.UI.UserControl
    | > | |
    | > | | #Region " Web Form Designer Generated Code "
    | > | |
    | > | | 'This call is required by the Web Form Designer.
    | > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | > | | InitializeComponent()
    | > | |
    | > | | End Sub
    | > | |
    | > | | Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    | > | | Protected WithEvents TextBox1 As
    System.Web.UI.WebControls.TextBox
    | > | |
    | > | | 'NOTE: The following placeholder declaration is required by the
    | Web
    | > | Form
    | > | | Designer.
    | > | | 'Do not delete or move it.
    | > | | Private designerPlaceholderDeclaration As System.Object
    | > | |
    | > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Init
    | > | | 'CODEGEN: This method call is required by the Web Form
    | Designer
    | > | | 'Do not modify it using the code editor.
    | > | | InitializeComponent()
    | > | | End Sub
    | > | |
    | > | | #End Region
    | > | |
    | > | | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Load
    | > | | 'Put user code to initialize the page here
    | > | | If IsPostBack Then Exit Sub
    | > | |
    | > | | With Label1
    | > | | .Width = Unit.Pixel(m_intLabelWidth)
    | > | | .Text = m_strLabel
    | > | | End With
    | > | |
    | > | | With TextBox1
    | > | | .Width = Unit.Pixel(m_intValueWidth)
    | > | | .Text = m_strValue
    | > | | .CssClass = "FieldEditableRequired"
    | > | | End With
    | > | |
    | > | | End Sub
    | > | |
    | > | | End Class
    | > | |
    | > | |
    | > | | WebForm1 HTML
    | > | | ==============
    | > | | <%@ Register TagPrefix="uc1" TagName="ctlFieldHEditableRequired"
    | > | | Src="ctlFieldHEditableRequired.ascx" %>
    | > | | <%@ Page Language="vb" AutoEventWireup="false"
    | > | Codebehind="WebForm1.aspx.vb"
    | > | | Inherits="UserControl.WebForm1"%>
    | > | | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    | > | | <HTML>
    | > | | <HEAD>
    | > | | <title>WebForm1</title>
    | > | | <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    | > | | <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    | > | | <meta name="vs_defaultClientScript" content="JavaScript">
    | > | | <meta name="vs_targetSchema"
    | > | | content="http://schemas.microsoft.com/intellisense/ie5">
    | > | | </HEAD>
    | > | | <body>
    | > | | <form id="Form1" method="post" runat="server">
    | > | | <P>
    | > | | <uc1:ctlFieldHEditableRequired id="ctlFieldHEditableRequired1"
    | > | | runat="server"></uc1:ctlFieldHEditableRequired></P>
    | > | | <P>
    | > | | <asp:Button id="Button1" runat="server"
    | > | Text="Button"></asp:Button></P>
    | > | | </form>
    | > | | </body>
    | > | | </HTML>
    | > | |
    | > | |
    | > | | WebForm1 CODEBEHIND
    | > | | ===================
    | > | | Public Class WebForm1
    | > | | Inherits System.Web.UI.Page
    | > | |
    | > | | #Region " Web Form Designer Generated Code "
    | > | |
    | > | | 'This call is required by the Web Form Designer.
    | > | | <System.Diagnostics.DebuggerStepThrough()> Private Sub
    | > | | InitializeComponent()
    | > | |
    | > | | End Sub
    | > | |
    | > | | Protected WithEvents ctlFieldHEditableRequired1 As
    | > | | ctlFieldHEditableRequired
    | > | | Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    | > | |
    | > | | 'NOTE: The following placeholder declaration is required by the
    | Web
    | > | Form
    | > | | Designer.
    | > | | 'Do not delete or move it.
    | > | | Private designerPlaceholderDeclaration As System.Object
    | > | |
    | > | | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Init
    | > | | 'CODEGEN: This method call is required by the Web Form
    | Designer
    | > | | 'Do not modify it using the code editor.
    | > | | InitializeComponent()
    | > | | End Sub
    | > | |
    | > | | #End Region
    | > | |
    | > | | Private Sub Page_Load(ByVal sender As Object, ByVal e As
    | > | | System.EventArgs) Handles MyBase.Load
    | > | |
    | > | | InitialiseControls()
    | > | |
    | > | | If Not IsPostBack Then
    | > | | GetProperties()
    | > | | End If
    | > | | End Sub
    | > | |
    | > | | Private Sub GetProperties()
    | > | |
    | > | | With ctlFieldHEditableRequired1
    | > | | .Label = "New Label"
    | > | | .Value = "New Value"
    | > | | End With
    | > | |
    | > | | End Sub
    | > | | Private Sub InitialiseControls()
    | > | |
    | > | | With ctlFieldHEditableRequired1
    | > | | .LabelWidth = 300
    | > | | .ValueWidth = 300
    | > | | .Label = "ID"
    | > | | End With
    | > | | End Sub
    | > | |
    | > | | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    | As
    | > | | System.EventArgs) Handles Button1.Click
    | > | | Response.Write(ctlFieldHEditableRequired1.Value)
    | > | | End Sub
    | > | | End Class
    | > | |
    | > | |
    | > | |
    | > |
    | > |
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 30, 2005
    #12
    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. Gibble
    Replies:
    0
    Views:
    482
    Gibble
    May 17, 2007
  2. Gibble
    Replies:
    0
    Views:
    341
    Gibble
    May 17, 2007
  3. Gibble
    Replies:
    0
    Views:
    335
    Gibble
    May 17, 2007
  4. Matt Sokol
    Replies:
    2
    Views:
    409
    Theo Tillotson
    Aug 7, 2003
  5. Terry Holland

    Dynamically adding a custom control

    Terry Holland, Nov 23, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    327
    Steven Cheng[MSFT]
    Nov 24, 2005
Loading...

Share This Page