SlideShare a Scribd company logo
1 of 19
   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).
   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
   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

Asp.net html server control
Asp.net html  server controlAsp.net html  server control
Asp.net html server controlSireesh K
 
MVC 3-RAZOR Validation
MVC 3-RAZOR ValidationMVC 3-RAZOR Validation
MVC 3-RAZOR ValidationKrunal Trivedi
 
Asp.net server controls
Asp.net server controlsAsp.net server controls
Asp.net server controlsRaed Aldahdooh
 
Kentico Connection 2014 Boston Upgrade Like a Pro
Kentico Connection 2014 Boston Upgrade Like a ProKentico Connection 2014 Boston Upgrade Like a Pro
Kentico Connection 2014 Boston Upgrade Like a ProBrian McKeiver
 
How to Wield Kentico 9 in the Real World
How to Wield Kentico 9 in the Real WorldHow to Wield Kentico 9 in the Real World
How to Wield Kentico 9 in the Real WorldBrian McKeiver
 
ASP.NET 04 - Additional Web Server Controls
ASP.NET 04 - Additional Web Server ControlsASP.NET 04 - Additional Web Server Controls
ASP.NET 04 - Additional Web Server ControlsRandy Connolly
 
Asp 2-createaspnetmvc
Asp 2-createaspnetmvcAsp 2-createaspnetmvc
Asp 2-createaspnetmvcFajar Baskoro
 
ASP.NET AJAX Basics
ASP.NET AJAX BasicsASP.NET AJAX Basics
ASP.NET AJAX Basicspetrov
 
Asp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentChui-Wen Chiu
 
Dog Food Con 2015 Integrate & Automate CMS Deployments
Dog Food Con 2015 Integrate & Automate CMS DeploymentsDog Food Con 2015 Integrate & Automate CMS Deployments
Dog Food Con 2015 Integrate & Automate CMS DeploymentsBrian McKeiver
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsRandy Connolly
 
Kentico 8 EMS API Deep Dive
Kentico 8 EMS API Deep DiveKentico 8 EMS API Deep Dive
Kentico 8 EMS API Deep DiveBrian McKeiver
 
Web api 2 With MVC 5 With TrainerKrunal
Web api 2 With MVC 5 With TrainerKrunalWeb api 2 With MVC 5 With TrainerKrunal
Web api 2 With MVC 5 With TrainerKrunalKrunal Trivedi
 
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012Wael Hamze
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kitVidhi Patel
 

What's hot (20)

MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
Web controls
Web controlsWeb controls
Web controls
 
Asp.net html server control
Asp.net html  server controlAsp.net html  server control
Asp.net html server control
 
MVC 3-RAZOR Validation
MVC 3-RAZOR ValidationMVC 3-RAZOR Validation
MVC 3-RAZOR Validation
 
Asp.net server controls
Asp.net server controlsAsp.net server controls
Asp.net server controls
 
Kentico Connection 2014 Boston Upgrade Like a Pro
Kentico Connection 2014 Boston Upgrade Like a ProKentico Connection 2014 Boston Upgrade Like a Pro
Kentico Connection 2014 Boston Upgrade Like a Pro
 
How to Wield Kentico 9 in the Real World
How to Wield Kentico 9 in the Real WorldHow to Wield Kentico 9 in the Real World
How to Wield Kentico 9 in the Real World
 
ASP.NET 04 - Additional Web Server Controls
ASP.NET 04 - Additional Web Server ControlsASP.NET 04 - Additional Web Server Controls
ASP.NET 04 - Additional Web Server Controls
 
Asp 2-createaspnetmvc
Asp 2-createaspnetmvcAsp 2-createaspnetmvc
Asp 2-createaspnetmvc
 
ASP.NET MVC 3
ASP.NET MVC 3ASP.NET MVC 3
ASP.NET MVC 3
 
ASP.NET AJAX Basics
ASP.NET AJAX BasicsASP.NET AJAX Basics
ASP.NET AJAX Basics
 
Asp.Net Ajax Component Development
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component Development
 
Dog Food Con 2015 Integrate & Automate CMS Deployments
Dog Food Con 2015 Integrate & Automate CMS DeploymentsDog Food Con 2015 Integrate & Automate CMS Deployments
Dog Food Con 2015 Integrate & Automate CMS Deployments
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server Controls
 
Kentico 8 EMS API Deep Dive
Kentico 8 EMS API Deep DiveKentico 8 EMS API Deep Dive
Kentico 8 EMS API Deep Dive
 
Ajax and ASP.NET AJAX
Ajax and ASP.NET AJAXAjax and ASP.NET AJAX
Ajax and ASP.NET AJAX
 
Web api 2 With MVC 5 With TrainerKrunal
Web api 2 With MVC 5 With TrainerKrunalWeb api 2 With MVC 5 With TrainerKrunal
Web api 2 With MVC 5 With TrainerKrunal
 
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
Silverlight & Microsoft CRM Development - extremeCRM Berlin 2012
 
ASP DOT NET
ASP DOT NETASP DOT NET
ASP DOT NET
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
 

Viewers also liked

Viewers also liked (19)

Ch3 server controls
Ch3 server controlsCh3 server controls
Ch3 server controls
 
Server Controls of ASP.Net
Server Controls of ASP.NetServer Controls of ASP.Net
Server Controls of ASP.Net
 
validations in asp .net
validations in asp .netvalidations in asp .net
validations in asp .net
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
 
Technical screening .Net Developer
Technical screening  .Net DeveloperTechnical screening  .Net Developer
Technical screening .Net Developer
 
ASP.NET - Web Programming
ASP.NET - Web ProgrammingASP.NET - Web Programming
ASP.NET - Web Programming
 
Introducing asp.net web pages 2
Introducing asp.net web pages 2Introducing asp.net web pages 2
Introducing asp.net web pages 2
 
Directives in asp.net
Directives in asp.netDirectives in asp.net
Directives in asp.net
 
ASP.NET Page life cycle and ViewState
ASP.NET Page life cycle and ViewStateASP.NET Page life cycle and ViewState
ASP.NET Page life cycle and ViewState
 
ASP.NET Web Security
ASP.NET Web SecurityASP.NET Web Security
ASP.NET Web Security
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
Presentation on asp.net controls
Presentation on asp.net controlsPresentation on asp.net controls
Presentation on asp.net controls
 
Intro To Asp Net And Web Forms
Intro To Asp Net And Web FormsIntro To Asp Net And Web Forms
Intro To Asp Net And Web Forms
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Controls in asp.net
Controls in asp.netControls in asp.net
Controls in asp.net
 
Validation controls in asp
Validation controls in aspValidation controls in asp
Validation controls in asp
 
ASP.NET Web API and HTTP Fundamentals
ASP.NET Web API and HTTP FundamentalsASP.NET Web API and HTTP Fundamentals
ASP.NET Web API and HTTP Fundamentals
 
ASPX Session xi(page lifecycle)
ASPX Session xi(page lifecycle)ASPX Session xi(page lifecycle)
ASPX Session xi(page lifecycle)
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView Tutorials
 

Similar to Custom controls

12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17Vivek chan
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010vchircu
 
Advanced VB: Object Oriented Programming - Controls
Advanced VB: Object Oriented Programming - ControlsAdvanced VB: Object Oriented Programming - Controls
Advanced VB: Object Oriented Programming - Controlsrobertbenard
 
Parallelminds.web partdemo1
Parallelminds.web partdemo1Parallelminds.web partdemo1
Parallelminds.web partdemo1parallelminder
 
Adding a view
Adding a viewAdding a view
Adding a viewNhan Do
 
Parallelminds.web partdemo
Parallelminds.web partdemoParallelminds.web partdemo
Parallelminds.web partdemoManishaChothe
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Vivek chan
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET AttributesPooja Gaikwad
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributessonia merchant
 
Aspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAlfa Gama Omega
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributessonia merchant
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentationBhavin Shah
 
GWT training session 2
GWT training session 2GWT training session 2
GWT training session 2SNEHAL MASNE
 
5a329780735625624 ch10
5a329780735625624 ch105a329780735625624 ch10
5a329780735625624 ch10harkesh singh
 

Similar to Custom controls (20)

12 asp.net session17
12 asp.net session1712 asp.net session17
12 asp.net session17
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
 
ASP.NET MVC3 RAD
ASP.NET MVC3 RADASP.NET MVC3 RAD
ASP.NET MVC3 RAD
 
Advanced VB: Object Oriented Programming - Controls
Advanced VB: Object Oriented Programming - ControlsAdvanced VB: Object Oriented Programming - Controls
Advanced VB: Object Oriented Programming - Controls
 
Parallelminds.web partdemo1
Parallelminds.web partdemo1Parallelminds.web partdemo1
Parallelminds.web partdemo1
 
Actionview
ActionviewActionview
Actionview
 
Adding a view
Adding a viewAdding a view
Adding a view
 
Parallelminds.web partdemo
Parallelminds.web partdemoParallelminds.web partdemo
Parallelminds.web partdemo
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
Asp.Net MVC Intro
Asp.Net MVC IntroAsp.Net MVC Intro
Asp.Net MVC Intro
 
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET Attributes
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
Aspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_cs
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
ASP.NET Lecture 6
ASP.NET Lecture 6ASP.NET Lecture 6
ASP.NET Lecture 6
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
GWT training session 2
GWT training session 2GWT training session 2
GWT training session 2
 
Mvc acchitecture
Mvc acchitectureMvc acchitecture
Mvc acchitecture
 
5a329780735625624 ch10
5a329780735625624 ch105a329780735625624 ch10
5a329780735625624 ch10
 

More from aspnet123

Deploying configuring caching
Deploying configuring cachingDeploying configuring caching
Deploying configuring cachingaspnet123
 
Mobile application
Mobile applicationMobile application
Mobile applicationaspnet123
 
Globalization and accessibility
Globalization and accessibilityGlobalization and accessibility
Globalization and accessibilityaspnet123
 
Monitoring, troubleshooting,
Monitoring, troubleshooting,Monitoring, troubleshooting,
Monitoring, troubleshooting,aspnet123
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 
User controls
User controlsUser controls
User controlsaspnet123
 
Web services
Web servicesWeb services
Web servicesaspnet123
 
Working with xml data
Working with xml dataWorking with xml data
Working with xml dataaspnet123
 
Connected data classes
Connected data classesConnected data classes
Connected data classesaspnet123
 
Disconnected data
Disconnected dataDisconnected data
Disconnected dataaspnet123
 
Introducing asp
Introducing aspIntroducing asp
Introducing aspaspnet123
 

More from aspnet123 (12)

Deploying configuring caching
Deploying configuring cachingDeploying configuring caching
Deploying configuring caching
 
Mobile application
Mobile applicationMobile application
Mobile application
 
Profile
ProfileProfile
Profile
 
Globalization and accessibility
Globalization and accessibilityGlobalization and accessibility
Globalization and accessibility
 
Monitoring, troubleshooting,
Monitoring, troubleshooting,Monitoring, troubleshooting,
Monitoring, troubleshooting,
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 
User controls
User controlsUser controls
User controls
 
Web services
Web servicesWeb services
Web services
 
Working with xml data
Working with xml dataWorking with xml data
Working with xml data
 
Connected data classes
Connected data classesConnected data classes
Connected data classes
 
Disconnected data
Disconnected dataDisconnected data
Disconnected data
 
Introducing asp
Introducing aspIntroducing asp
Introducing asp
 

Recently uploaded

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Recently uploaded (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Custom controls

  • 1.
  • 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).
  • 10.
  • 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
  • 12.
  • 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.