Re: Dynamically adding custom control



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" <terryeholland@xxxxxxxxxxxxxxxx>
| References: <#l8Ze376FHA.1188@xxxxxxxxxxxxxxxxxxxx>
<jlvfhG$6FHA.832@xxxxxxxxxxxxxxxxxxxxx>
| 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: <OCSgHKF7FHA.1416@xxxxxxxxxxxxxxxxxxxx>
| 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]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
| news:jlvfhG$6FHA.832@xxxxxxxxxxxxxxxxxxxxxxxx
| > 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" <terryeholland@xxxxxxxxxxxxxxxx>
| > | 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: <#l8Ze376FHA.1188@xxxxxxxxxxxxxxxxxxxx>
| > | 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
| > | ===============================
| > |
| > |
| > |
| >
|
|
|

.



Relevant Pages

  • Re: IsPostBack and usercontrols
    ... Microsoft Online Support ... | End Sub 'LoadTab ... |> Private ContentControl As Control ... Since there exists Data Load code in the Control's internal code ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Dynamically adding custom control
    ... As for ASP.NET Usercontrol(ascx...), the Control instances are all defined ... | 'This call is required by the Web Form Designer. ... | Private Sub ... | Private Sub Page_Init(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Dynamically adding custom control
    ... method in my page to load the control and now the user control appears on ... > | 'This call is required by the Web Form Designer. ... > | Private Sub ... > | Private Sub Page_Init(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Dynamically adding custom control
    ... > concrete defination in sub classes, you can declare them as "Overridable ... > | Subject: Re: Dynamically adding custom control ... > | | Private Sub Page_Init(ByVal sender As System.Object, ... Designer ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: CreateChildControls(...) or Render(...)?
    ... You can simply associate your control to a custom control designer class ... method into another sub and have your createchildcontrols method call this. ... This way you can call this new sub from your designer class and render the ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)