SlideShare a Scribd company logo
Custom controls
   A custom Web control is a control that inherits
    from a WebServer control.

   A custom Web control can be compiled into a
    separate .dll file that can be shared among
    applications.

   It allows to define a better design-time
    experience for the consumers of the control.

   This includes Toolbox and designer functionality
There are two common approaches to creating a
    custom Web server control.

   The first approach is to create a Web server control that
    inherits directly from WebControl Class. The Web-
    Control class provides a base set of functionality.
    This leaves a lot of work to develop the control.

   The second approach is to inherit from an existing Web
    control that already provides the core features of your
    control.
    This can give you a jump start and allow you to focus
    on what makes your control different.
    This is the more common scenario.
   If the custom Web server control is targeted to multiple
    Web sites, you should place the new custom Web
    server control class into a class library project to create
    a .dll file that can be shared.

   If the custom Web server control is only meant for the
    current Web site, you can add the custom Web server
    control’s class file to the Web site.
   For example, suppose you wish to create a custom version of the
    TextBox control. This custom version will include text that labels
    the given TextBox (such as “User Name” or “Password”).
   You create this control by first inheriting from TextBox.

    public class LabeledTextBox : TextBox
    {
    public string PromptText { get; set; }
    public int PromptWidth { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
    writer.Write( @"<span style="”display:inline-
    block;width:{0}px”">{1}&nbsp;</span>",
    PromptWidth, PromptText);

    base.Render(writer);
    }
    }
   This approach is desirable when there is no
    control that currently provides default
    behavior similar to the control you wish to
    implement.
   When inheriting from the WebControl class,
    you must override the Render method to
    provide the desired output.
   For example, suppose you wish to create a
    custom control that allows a user to display a
    logo and an associated company name for
    the logo. For this, you might create two
    properties: LogoUrl and CompanyName.
public class LogoControl : WebControl {
public LogoControl(){}
public string LogoUrl     {
get { return _logoUrl; }
set { _logoUrl = value; } }
private string _logoUrl;
public string CompanyName         {
get { return _companyName; }
set { _companyName = value; }     }
private string _companyName;
protected override void Render(HtmlTextWriter writer) {
writer.WriteFullBeginTag(“div”);
writer.Write(@"<img src=""{0}"" /><br />", LogoUrl);
writer.Write(CompanyName + "<br />");
writer.WriteEndTag(“div”);
}
}
   The primary requirement to allow a custom Web
    control to be added to the Toolbox is that the Web
    control be placed into a separate .dll file.

   You can set a reference to this .dll file to add the
    .dll to the Bin folder of your project.

   In this case, you can right-click the Toolbox and
    select Choose Items. You can then browse to the
    given user control’s .dll to add the controls that
    are contained in the file to the Toolbox.
   Notice that the controls are defined in their own
    grouping at the top of the Toolbox.

   When you drag a control to the page, the control
    must be registered.

   This markup is shown at the top of the page.

   The bottom of the page shows the control
    defined declaratively (including custom
    properties).
Custom controls
   You can add your own custom icon through an
    attribute called ToolboxBitmap of the System.Drawing
    namespace.
   This attribute specifies a bitmap that is 16 × 16 pixels in
    size.
    [ToolboxBitmap(typeof(LabeledTextBox),
    “MyUserControls.LabeledTextBox.bmp”)]
    public class LabeledTextBox : TextBox
Custom controls
   A default property is accessed without actually
    specifying a property.
   You set the default property for your control through
    the DefaultProperty attribute class in the
    System.ComponentModel namespace.
   Apply this attribute at the class level.
   Simply pass the name of one of your properties to this
    attribute.

    [DefaultProperty(“PromptText”)]
    public class LabeledTextBox : TextBox
   You can further change the way your custom
    server control behaves when it is dropped onto
    the Web page by setting the ToolboxDataAttribute
    in your control class.

   This attribute is used to change the markup that
    gets generated by Visual Studio.

   A common scenario is to set default values for
    properties on the control inside the generated
    markup.
   You might wish to alter the default rendering of the control
    in design mode.

   To do so, you start by adding a reference to the
    System.Design.dll assembly in your user control.

   You then create a new class in your user control that
    inherits from the ControlDesigner class. This class will
    override the GetDesignTimeHtml method of the
    ControlDesigner class to render separate design-time HTML
    that can be set based on the property settings of the control
    instance.

   You then apply the Designer attribute to your control.

   To this, you pass an instance of your ControlDesigner class.
[Designer(“MyUserControls.LabeledTextBoxDesigner, MyUserControls”)]
public class LabeledTextBoxDesigner : ControlDesigner
{
private LabeledTextBox _labeledTextBoxControl;
public override string GetDesignTimeHtml()
{
if (_labeledTextBoxControl.PromptText.Trim().Length == 0)
return "<div style='color: Gray'>[Define PromptText]</div>";
else
return base.GetDesignTimeHtml();
}
public override void Initialize(IComponent component)
{
_labeledTextBoxControl = (LabeledTextBox)component;
base.Initialize(component);
return;
}}
   A composite control is a custom Web control that
    contains other controls.

   This sounds like a user control, but the composite
    control doesn’t provide a designer screen for creating
    the control, nor an .ascx file that lets you drag and
    drop controls on it at design time.

   Instead, you create a custom control that inherits
    from the CompositeControl class. You then add
    constituent controls to this control.

   The composite control then handles events raised by
    its child controls.
   To create a composite control, you start by creating a
    class that inherits from the CompositeControl class and
    overrides the CreateChildControls method.

   The CreateChildControls method contains the code to
    instantiate the child controls and set their properties.

   If you want to be able to assign styles to the composite
    control, you should create an instance of the Panel class
    to provide a container that can have attributes assigned
    to it.

   In this case, you add it to the Controls collection of your
    composite control, and then add your controls to the
    Panel control.
   A templated custom Web control provides separation
    of control data from its presentation.

   This means that a templated control does not provide
    a default UI.

   For example, if you know that you need to display
    product data, but you don’t know how the developer
    who intends to use your control wants to format the
    product data, you could create a templated control
    that allows the page designer to supply the format for
    the product data as a template.

More Related Content

PPT
Custom Controls in ASP.net
PPTX
Custom control in asp.net
PPTX
Client control
PPTX
Ajax control asp.net
PDF
Visual studio 2008 asp net
PPTX
MVC Training Part 2
PPT
Introduction To Asp.Net Ajax
Custom Controls in ASP.net
Custom control in asp.net
Client control
Ajax control asp.net
Visual studio 2008 asp net
MVC Training Part 2
Introduction To Asp.Net Ajax

What's hot (20)

PPT
MSDN - ASP.NET MVC
PPT
Web controls
PPTX
Asp.net html server control
PPTX
MVC 3-RAZOR Validation
PPT
Asp.net server controls
PPTX
Kentico Connection 2014 Boston Upgrade Like a Pro
PPTX
How to Wield Kentico 9 in the Real World
PPT
ASP.NET 04 - Additional Web Server Controls
PDF
Asp 2-createaspnetmvc
PDF
ASP.NET MVC 3
PPT
ASP.NET AJAX Basics
PPT
Asp.Net Ajax Component Development
PPTX
Dog Food Con 2015 Integrate & Automate CMS Deployments
PPT
ASP.NET 03 - Working With Web Server Controls
PPTX
Kentico 8 EMS API Deep Dive
PPTX
Ajax and ASP.NET AJAX
PPTX
Web api 2 With MVC 5 With TrainerKrunal
PDF
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
PPTX
ASP DOT NET
PPTX
Ajax control tool kit
MSDN - ASP.NET MVC
Web controls
Asp.net html server control
MVC 3-RAZOR Validation
Asp.net server controls
Kentico Connection 2014 Boston Upgrade Like a Pro
How to Wield Kentico 9 in the Real World
ASP.NET 04 - Additional Web Server Controls
Asp 2-createaspnetmvc
ASP.NET MVC 3
ASP.NET AJAX Basics
Asp.Net Ajax Component Development
Dog Food Con 2015 Integrate & Automate CMS Deployments
ASP.NET 03 - Working With Web Server Controls
Kentico 8 EMS API Deep Dive
Ajax and ASP.NET AJAX
Web api 2 With MVC 5 With TrainerKrunal
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
ASP DOT NET
Ajax control tool kit
Ad

Viewers also liked (19)

PPTX
Ch3 server controls
PPT
Server Controls of ASP.Net
PPTX
validations in asp .net
PPTX
ASP .NET MVC
PDF
Technical screening .Net Developer
PDF
Introducing asp.net web pages 2
PPTX
Directives in asp.net
PDF
ASP.NET Page life cycle and ViewState
PPTX
ASP.NET Web Security
PPTX
ASP.NET Page Life Cycle
PPTX
Presentation on asp.net controls
PPT
Intro To Asp Net And Web Forms
PPTX
Web forms in ASP.net
PPTX
Validation controls in asp
PPTX
ASP.NET Web API and HTTP Fundamentals
PPT
ASPX Session xi(page lifecycle)
PDF
Step by Step Asp.Net GridView Tutorials
Ch3 server controls
Server Controls of ASP.Net
validations in asp .net
ASP .NET MVC
Technical screening .Net Developer
Introducing asp.net web pages 2
Directives in asp.net
ASP.NET Page life cycle and ViewState
ASP.NET Web Security
ASP.NET Page Life Cycle
Presentation on asp.net controls
Intro To Asp Net And Web Forms
Web forms in ASP.net
Validation controls in asp
ASP.NET Web API and HTTP Fundamentals
ASPX Session xi(page lifecycle)
Step by Step Asp.Net GridView Tutorials
Ad

Similar to Custom controls (20)

PPSX
12 asp.net session17
PDF
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
DOCX
ASP.NET MVC3 RAD
PPTX
Advanced VB: Object Oriented Programming - Controls
PPTX
Parallelminds.web partdemo1
PPS
Actionview
DOCX
Adding a view
PPTX
Parallelminds.web partdemo
PPSX
03 asp.net session04
PPTX
Asp.Net MVC Intro
PPTX
Asp PPT (.NET )
PPT
Learn dot net attributes
PPT
Learning .NET Attributes
PDF
Aspnet mvc tutorial_01_cs
PDF
Learn about dot net attributes
PPTX
ASP.NET Lecture 6
PPT
MVC ppt presentation
PDF
GWT training session 2
PDF
Mvc acchitecture
PDF
5a329780735625624 ch10
12 asp.net session17
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ASP.NET MVC3 RAD
Advanced VB: Object Oriented Programming - Controls
Parallelminds.web partdemo1
Actionview
Adding a view
Parallelminds.web partdemo
03 asp.net session04
Asp.Net MVC Intro
Asp PPT (.NET )
Learn dot net attributes
Learning .NET Attributes
Aspnet mvc tutorial_01_cs
Learn about dot net attributes
ASP.NET Lecture 6
MVC ppt presentation
GWT training session 2
Mvc acchitecture
5a329780735625624 ch10

More from aspnet123 (12)

PPTX
Deploying configuring caching
PPTX
Mobile application
PPTX
Profile
PPTX
Globalization and accessibility
PPTX
Monitoring, troubleshooting,
PPTX
Programming web application
PPTX
User controls
PPTX
Web services
PPTX
Working with xml data
PPTX
Connected data classes
PPTX
Disconnected data
PPTX
Introducing asp
Deploying configuring caching
Mobile application
Profile
Globalization and accessibility
Monitoring, troubleshooting,
Programming web application
User controls
Web services
Working with xml data
Connected data classes
Disconnected data
Introducing asp

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Weekly Chronicles - August'25 Week I
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity

Custom controls

  • 2. A custom Web control is a control that inherits from a WebServer control.  A custom Web control can be compiled into a separate .dll file that can be shared among applications.  It allows to define a better design-time experience for the consumers of the control.  This includes Toolbox and designer functionality
  • 3. There are two common approaches to creating a custom Web server control.  The first approach is to create a Web server control that inherits directly from WebControl Class. The Web- Control class provides a base set of functionality. This leaves a lot of work to develop the control.  The second approach is to inherit from an existing Web control that already provides the core features of your control. This can give you a jump start and allow you to focus on what makes your control different. This is the more common scenario.
  • 4. If the custom Web server control is targeted to multiple Web sites, you should place the new custom Web server control class into a class library project to create a .dll file that can be shared.  If the custom Web server control is only meant for the current Web site, you can add the custom Web server control’s class file to the Web site.
  • 5. For example, suppose you wish to create a custom version of the TextBox control. This custom version will include text that labels the given TextBox (such as “User Name” or “Password”).  You create this control by first inheriting from TextBox. public class LabeledTextBox : TextBox { public string PromptText { get; set; } public int PromptWidth { get; set; } protected override void Render(HtmlTextWriter writer) { writer.Write( @"<span style="”display:inline- block;width:{0}px”">{1}&nbsp;</span>", PromptWidth, PromptText); base.Render(writer); } }
  • 6. This approach is desirable when there is no control that currently provides default behavior similar to the control you wish to implement.  When inheriting from the WebControl class, you must override the Render method to provide the desired output.  For example, suppose you wish to create a custom control that allows a user to display a logo and an associated company name for the logo. For this, you might create two properties: LogoUrl and CompanyName.
  • 7. public class LogoControl : WebControl { public LogoControl(){} public string LogoUrl { get { return _logoUrl; } set { _logoUrl = value; } } private string _logoUrl; public string CompanyName { get { return _companyName; } set { _companyName = value; } } private string _companyName; protected override void Render(HtmlTextWriter writer) { writer.WriteFullBeginTag(“div”); writer.Write(@"<img src=""{0}"" /><br />", LogoUrl); writer.Write(CompanyName + "<br />"); writer.WriteEndTag(“div”); } }
  • 8. The primary requirement to allow a custom Web control to be added to the Toolbox is that the Web control be placed into a separate .dll file.  You can set a reference to this .dll file to add the .dll to the Bin folder of your project.  In this case, you can right-click the Toolbox and select Choose Items. You can then browse to the given user control’s .dll to add the controls that are contained in the file to the Toolbox.
  • 9. Notice that the controls are defined in their own grouping at the top of the Toolbox.  When you drag a control to the page, the control must be registered.  This markup is shown at the top of the page.  The bottom of the page shows the control defined declaratively (including custom properties).
  • 11. You can add your own custom icon through an attribute called ToolboxBitmap of the System.Drawing namespace.  This attribute specifies a bitmap that is 16 × 16 pixels in size. [ToolboxBitmap(typeof(LabeledTextBox), “MyUserControls.LabeledTextBox.bmp”)] public class LabeledTextBox : TextBox
  • 13. A default property is accessed without actually specifying a property.  You set the default property for your control through the DefaultProperty attribute class in the System.ComponentModel namespace.  Apply this attribute at the class level.  Simply pass the name of one of your properties to this attribute. [DefaultProperty(“PromptText”)] public class LabeledTextBox : TextBox
  • 14. You can further change the way your custom server control behaves when it is dropped onto the Web page by setting the ToolboxDataAttribute in your control class.  This attribute is used to change the markup that gets generated by Visual Studio.  A common scenario is to set default values for properties on the control inside the generated markup.
  • 15. You might wish to alter the default rendering of the control in design mode.  To do so, you start by adding a reference to the System.Design.dll assembly in your user control.  You then create a new class in your user control that inherits from the ControlDesigner class. This class will override the GetDesignTimeHtml method of the ControlDesigner class to render separate design-time HTML that can be set based on the property settings of the control instance.  You then apply the Designer attribute to your control.  To this, you pass an instance of your ControlDesigner class.
  • 16. [Designer(“MyUserControls.LabeledTextBoxDesigner, MyUserControls”)] public class LabeledTextBoxDesigner : ControlDesigner { private LabeledTextBox _labeledTextBoxControl; public override string GetDesignTimeHtml() { if (_labeledTextBoxControl.PromptText.Trim().Length == 0) return "<div style='color: Gray'>[Define PromptText]</div>"; else return base.GetDesignTimeHtml(); } public override void Initialize(IComponent component) { _labeledTextBoxControl = (LabeledTextBox)component; base.Initialize(component); return; }}
  • 17. A composite control is a custom Web control that contains other controls.  This sounds like a user control, but the composite control doesn’t provide a designer screen for creating the control, nor an .ascx file that lets you drag and drop controls on it at design time.  Instead, you create a custom control that inherits from the CompositeControl class. You then add constituent controls to this control.  The composite control then handles events raised by its child controls.
  • 18. To create a composite control, you start by creating a class that inherits from the CompositeControl class and overrides the CreateChildControls method.  The CreateChildControls method contains the code to instantiate the child controls and set their properties.  If you want to be able to assign styles to the composite control, you should create an instance of the Panel class to provide a container that can have attributes assigned to it.  In this case, you add it to the Controls collection of your composite control, and then add your controls to the Panel control.
  • 19. A templated custom Web control provides separation of control data from its presentation.  This means that a templated control does not provide a default UI.  For example, if you know that you need to display product data, but you don’t know how the developer who intends to use your control wants to format the product data, you could create a templated control that allows the page designer to supply the format for the product data as a template.