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

What's hot (20)

PPT
MSDN - ASP.NET MVC
Maarten Balliauw
 
PPT
Web controls
Sarthak Varshney
 
PPTX
Asp.net html server control
Sireesh K
 
PPTX
MVC 3-RAZOR Validation
Krunal Trivedi
 
PPT
Asp.net server controls
Raed Aldahdooh
 
PPTX
Kentico Connection 2014 Boston Upgrade Like a Pro
Brian McKeiver
 
PPTX
How to Wield Kentico 9 in the Real World
Brian McKeiver
 
PPT
ASP.NET 04 - Additional Web Server Controls
Randy Connolly
 
PDF
Asp 2-createaspnetmvc
Fajar Baskoro
 
PDF
ASP.NET MVC 3
Buu Nguyen
 
PPT
ASP.NET AJAX Basics
petrov
 
PPT
Asp.Net Ajax Component Development
Chui-Wen Chiu
 
PPTX
Dog Food Con 2015 Integrate & Automate CMS Deployments
Brian McKeiver
 
PPT
ASP.NET 03 - Working With Web Server Controls
Randy Connolly
 
PPTX
Kentico 8 EMS API Deep Dive
Brian McKeiver
 
PPTX
Ajax and ASP.NET AJAX
Julie Iskander
 
PPTX
Web api 2 With MVC 5 With TrainerKrunal
Krunal Trivedi
 
PDF
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Wael Hamze
 
PPTX
ASP DOT NET
Pratik Tambekar
 
PPTX
Ajax control tool kit
Vidhi Patel
 
MSDN - ASP.NET MVC
Maarten Balliauw
 
Web controls
Sarthak Varshney
 
Asp.net html server control
Sireesh K
 
MVC 3-RAZOR Validation
Krunal Trivedi
 
Asp.net server controls
Raed Aldahdooh
 
Kentico Connection 2014 Boston Upgrade Like a Pro
Brian McKeiver
 
How to Wield Kentico 9 in the Real World
Brian McKeiver
 
ASP.NET 04 - Additional Web Server Controls
Randy Connolly
 
Asp 2-createaspnetmvc
Fajar Baskoro
 
ASP.NET MVC 3
Buu Nguyen
 
ASP.NET AJAX Basics
petrov
 
Asp.Net Ajax Component Development
Chui-Wen Chiu
 
Dog Food Con 2015 Integrate & Automate CMS Deployments
Brian McKeiver
 
ASP.NET 03 - Working With Web Server Controls
Randy Connolly
 
Kentico 8 EMS API Deep Dive
Brian McKeiver
 
Ajax and ASP.NET AJAX
Julie Iskander
 
Web api 2 With MVC 5 With TrainerKrunal
Krunal Trivedi
 
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Wael Hamze
 
ASP DOT NET
Pratik Tambekar
 
Ajax control tool kit
Vidhi Patel
 

Viewers also liked (19)

PPTX
Ch3 server controls
Madhuri Kavade
 
PPT
Server Controls of ASP.Net
Hitesh Santani
 
PPTX
validations in asp .net
Akshay Thakre
 
PPTX
ASP .NET MVC
eldorina
 
PDF
Technical screening .Net Developer
Tom Henricksen
 
PDF
Introducing asp.net web pages 2
Uh-meet Thapa
 
PPTX
Directives in asp.net
Sireesh K
 
PDF
ASP.NET Page life cycle and ViewState
Mindfire Solutions
 
PPTX
ASP.NET Web Security
SharePointRadi
 
PPTX
ASP.NET Page Life Cycle
Abhishek Sur
 
PPTX
Presentation on asp.net controls
Reshi Unen
 
PPT
Intro To Asp Net And Web Forms
SAMIR BHOGAYTA
 
PPTX
Web forms in ASP.net
Madhuri Kavade
 
PPTX
Validation controls in asp
Shishir Jain
 
PPTX
ASP.NET Web API and HTTP Fundamentals
Ido Flatow
 
PPT
ASPX Session xi(page lifecycle)
Shrijan Tiwari
 
PDF
Step by Step Asp.Net GridView Tutorials
Nilesh kumar Jadav
 
Ch3 server controls
Madhuri Kavade
 
Server Controls of ASP.Net
Hitesh Santani
 
validations in asp .net
Akshay Thakre
 
ASP .NET MVC
eldorina
 
Technical screening .Net Developer
Tom Henricksen
 
Introducing asp.net web pages 2
Uh-meet Thapa
 
Directives in asp.net
Sireesh K
 
ASP.NET Page life cycle and ViewState
Mindfire Solutions
 
ASP.NET Web Security
SharePointRadi
 
ASP.NET Page Life Cycle
Abhishek Sur
 
Presentation on asp.net controls
Reshi Unen
 
Intro To Asp Net And Web Forms
SAMIR BHOGAYTA
 
Web forms in ASP.net
Madhuri Kavade
 
Validation controls in asp
Shishir Jain
 
ASP.NET Web API and HTTP Fundamentals
Ido Flatow
 
ASPX Session xi(page lifecycle)
Shrijan Tiwari
 
Step by Step Asp.Net GridView Tutorials
Nilesh kumar Jadav
 
Ad

Similar to Custom controls (20)

PPSX
12 asp.net session17
Vivek Singh Chandel
 
PDF
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
vchircu
 
DOCX
ASP.NET MVC3 RAD
Mădălin Ștefîrcă
 
PPTX
Advanced VB: Object Oriented Programming - Controls
robertbenard
 
PPTX
Parallelminds.web partdemo1
parallelminder
 
PPS
Actionview
Amal Subhash
 
DOCX
Adding a view
Nhan Do
 
PPTX
Parallelminds.web partdemo
ManishaChothe
 
PPSX
03 asp.net session04
Vivek Singh Chandel
 
PPTX
Asp.Net MVC Intro
Stefano Paluello
 
PPTX
Asp PPT (.NET )
Ankit Gupta
 
PPT
Learning .NET Attributes
Pooja Gaikwad
 
PPT
Learn dot net attributes
sonia merchant
 
PDF
Aspnet mvc tutorial_01_cs
Alfa Gama Omega
 
PDF
Learn about dot net attributes
sonia merchant
 
PPTX
ASP.NET Lecture 6
Julie Iskander
 
PPT
MVC ppt presentation
Bhavin Shah
 
PDF
GWT training session 2
SNEHAL MASNE
 
PDF
Mvc acchitecture
laxmi.katkar
 
PDF
5a329780735625624 ch10
harkesh singh
 
12 asp.net session17
Vivek Singh Chandel
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
vchircu
 
ASP.NET MVC3 RAD
Mădălin Ștefîrcă
 
Advanced VB: Object Oriented Programming - Controls
robertbenard
 
Parallelminds.web partdemo1
parallelminder
 
Actionview
Amal Subhash
 
Adding a view
Nhan Do
 
Parallelminds.web partdemo
ManishaChothe
 
03 asp.net session04
Vivek Singh Chandel
 
Asp.Net MVC Intro
Stefano Paluello
 
Asp PPT (.NET )
Ankit Gupta
 
Learning .NET Attributes
Pooja Gaikwad
 
Learn dot net attributes
sonia merchant
 
Aspnet mvc tutorial_01_cs
Alfa Gama Omega
 
Learn about dot net attributes
sonia merchant
 
ASP.NET Lecture 6
Julie Iskander
 
MVC ppt presentation
Bhavin Shah
 
GWT training session 2
SNEHAL MASNE
 
Mvc acchitecture
laxmi.katkar
 
5a329780735625624 ch10
harkesh singh
 
Ad

More from aspnet123 (12)

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

Recently uploaded (20)

PDF
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
PDF
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
PDF
CIFDAQ'S Token Spotlight for 16th July 2025 - ALGORAND
CIFDAQ
 
PPTX
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
"Effect, Fiber & Schema: tactical and technical characteristics of Effect.ts"...
Fwdays
 
PDF
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
PDF
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
PDF
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
PDF
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
PPTX
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
PPTX
TYPES OF COMMUNICATION Presentation of ICT
JulieBinwag
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Productivity Management Software | Workstatus
Lovely Baghel
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
CIFDAQ'S Token Spotlight for 16th July 2025 - ALGORAND
CIFDAQ
 
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Top Managed Service Providers in Los Angeles
Captain IT
 
"Effect, Fiber & Schema: tactical and technical characteristics of Effect.ts"...
Fwdays
 
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
TYPES OF COMMUNICATION Presentation of ICT
JulieBinwag
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Productivity Management Software | Workstatus
Lovely Baghel
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 

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.