22
201404100120001 CHAUHAN SUSIL 201404100120002 DESAI CHAITANYA 201404100120014 PATEL SHUBHAM Custom Server Control

custom server control

Embed Size (px)

Citation preview

Page 1: custom server control

201404100120001 CHAUHAN SUSIL201404100120002 DESAI

CHAITANYA201404100120014 PATEL SHUBHAM

Custom Server Control

Page 2: custom server control

What is Server Control?

A server control is a .Net component that is used to generate the user interface of ASP.Net web application.

It is implemented as managed class deriving directly or indirectly from System.Web.UI.Control base class.

Page 3: custom server control

Type of Controls

User Control

Custom Control

Page 4: custom server control

User control

A user control is a small section of a page that can include static HTML code and web server controls

User Control is use to create a new web application and web form.

Easy to createEasy to useLack support for design-time featuresScoped to a single applicationVery similar to creating an ASPX page

Page 5: custom server control

Cont.…..

User control (.ascx) files are similar to ASP.NET web-form (.aspx) files. Like web forms, user controls are

composed of a user interface portion with control tags (the .ascx file) and can use inline script or a .cs

code-behind file. User controls can contain just about anything a web page can, including static HTML

User controls begin with a Control directive instead of a Page directive.

Page 6: custom server control

Cont.…..

User controls use the file extension .ascx instead of .aspx, and their code-behind

Files inherit from the System.Web.UI.UserControl class. In fact, the User Control

Class and the Page class both inherit from the same Template Control class, which is why they share so many of the same methods and events.

Page 7: custom server control

Cont.…..

User controls can’t be requested directly by a client browser. (ASP.NET will give a generic “that file type is not served” error message to anyone who tries.) Instead,

User controls are embedded inside other web pages.

Page 8: custom server control

Creating a Simple User Control

To create a user control in Visual Studio, select Website Add New Item, and choose ➤the Web User

Control template.The following is the simplest possible user

control—one that merely contains static HTML. This user

control represents a header bar.

Page 9: custom server control

Cont.…..

User controls are.<h1></h1> <font ></font><table></table>

Page 10: custom server control

Cont.…..

Example.<h1 align=“center”>Welcome To Home</h1><font color=“red”>My Pages</font><table align=“center” cellspacing=“10”

cellpadding=“10” border=“10”><tr><td><b>Example</b></td></tr></table>

Page 11: custom server control

Advantages

User controls is that once you create one, you can reuse it in multiple pages in the same web application.

You can even add your own properties, events, and methods.

Page 12: custom server control

Disadvantages

• It do not support template.• Less flexibility.• Less performance.• Less designer support.

Page 13: custom server control

Custom Server Control

Creating your own controls can simultaneously improve the quality of Web applications, make your more productive and improve your user interface.

Custom server controls are not placed in /App_Code folder but compiled to dll.

You need to write complete control in code and there is no some visual designer (no markup code and no .ascx file).

Page 14: custom server control

How to create custom server control?

In Visual Studio, create a new project. Choose the "ASP.NET Server Control" type, or "Web Custom Control" type.

Visual Studio will create a class that inherits from WebControl class, which is located in System.Web.UI.WebControls interface.

Page 15: custom server control

How to place custom server control on web pages

First, your custom server control's dll must be in web application /bin folder.

After that, you can add custom controls to web page in two simple steps:

First, you need to register custom controls at the top of the markup code (.aspx file) by using Register directive. You can do this with markup code like this.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"Inherits="_Default" %><%@ Register Namespace="BeanSoftware" Assembly="MyAssembly"TagPrefix="cc" %>

Page 16: custom server control

Second, on the place where you want to add custom control, use TagPrefix property value and class name of your custom control to build a tag, like this:

<cc:MyClassName runat="server" ID="MyControl1" />

So, tag is built in this form Tag Prefix: CustomControlClassName. Except this, you should add at least runat="server" and specify ID of control.

Page 17: custom server control

It is possible to build three types of web custom server controls in ASP.NET:

1. Derived custom controls

2. Composite controls

3. Custom rendered controls

Page 18: custom server control

Derived Custom Server Control

If you want to extend existing control and add some new features, you can build derived custom control. When you create new server control, Visual Studio by default creates class that inherits from WebControl class.

 You can change this and inherit any other control. For example, to create server control inherited from TextBox, you should use code like this:

public class ServerControl1 : TextBox {  }

Page 19: custom server control

Control created on this way will have all functionality of based control. In this example, we'll get a complete TextBox control and now we need to add just code for additional features. It is possible to create new properties, methods and events or to override existing.

Page 20: custom server control

Composite Server Control

Composite control is a control that are built from other controls. To build composite control you need to inherit WebControl class and implement INamingContainer interface. 

This interface have not any implementation, but it is needed to subcontrols get unique names on web page. After that, override ControlCollection property and CreateChildControls method.

In ControlCollection property just call EnsureChildControls() method. EnsureChildControls method checks if CreateChildControls is already called, and call it if not.

Page 21: custom server control

Custom Rendered server control

In custom rendered server controls you need to build complete output. First, you need to override RenderContents method. Then, use HtmlTextWriter object to create control's UI. In some basic example let output of control be text colored in red, RenderContents method could look like this:

protected override void RenderContents(HtmlTextWriter output) {   output.AddStyleAttribute(HtmlTextWriterStyle.Color, "red");   output.RenderBeginTag(HtmlTextWriterTag.Span);   output.Write("This text will be red");   output.RenderEndTag(); }

Page 22: custom server control

THANK YOU