User Control
The syntax you use to create a user control is similar to the syntax you use to create a Web Forms page (.aspx). The only difference is that a user control does not include the html, form and body elements since a Web Forms page hosts the user control.
Now you can use control on page like '<tp:MyUC ID ="myFirstUc" runat="server" />'
Add user control to page at run time.
// Load the control by calling LoadControl on the page class.
Control c1 = LoadControl("test.ascx");
// Add the loaded control in the page controls collection.
Page.Controls.Add(c1);
Custom control
Custom control
Custom controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does.
In new project window select in web select ASP.net server control and then write code in control's class. Now you can override render method of control to display as you want on rendering of control.
namespace FirstCustomControl {
[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomEmailLink runat=server></{0}:CustomEmailLink>")]
public class CustomEmailLink : WebControl {
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string DisplayName {
get {
String s = (String)ViewState["DisplayName"];
return ((s == null) ? "[DisplayName]" : s);
}
set {
ViewState["DisplayName"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string Email {
get {
String s = (String)ViewState["Email"];
return ((s == null) ? "[Email]" : s);
}
set {
ViewState["Email"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string Subject {
get {
String s = (String)ViewState["Subject"];
return ((s == null) ? string.Empty : s);
}
set {
ViewState["Subject"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string Body {
get {
String s = (String)ViewState["Body"];
return ((s == null) ?string.Empty : s);
}
set {
ViewState["Body"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output) {
if (this.DesignMode) { RenderDesign(output); } else {
if (this.Visible) {
RenderRuntime(output);
}
else {
return;
}
}
}
private void RenderRuntime(HtmlTextWriter output) {
HyperLink link = new HyperLink();
link.ID = this.ID;
link.Text = this.DisplayName;
link.NavigateUrl=string.Format("mailto:{0}",this.Email);
if (!string.IsNullOrEmpty(Subject)) {
link.NavigateUrl = link.NavigateUrl + "?subject=" + this.Subject;
link.NavigateUrl = link.NavigateUrl + "&body=" + this.Body;
}
link.RenderControl(output);
}
private void RenderDesign(HtmlTextWriter output) {
output.Write("Display Name:{0}<br/>Email:{1}<br/>Subject:{2}<br/>Body:{3}", this.DisplayName, this.Email,this.Subject,this.Body);
}
}
}
Right click on tool box select 'choose items', then select .net components, then browse custom control's dll. Now you can use this custom control on pages like other controls.
User control Vs Custom Control 1. You can use custom control in your application by adding assembly reference to application while you need separate copy of user control in each application.
2. You can add custom control in tool box while user control can’t be added.
3. Custom control can be used in multiple applications by adding assembly to the GAC for same machine.
4. Custom controls are difficult to create but easy to use in multiple applications. 5.
User controls are easy to create but difficult to use in multiple application.
No comments:
Post a Comment