Welcome to weblogs.com.pk Sign in | Join | Help

ASP.NET Server Controls

ASP.NET provides wide spectrum; you can develop reusable interface components using User Controls, Server Controls and Web Parts (WebForms world). There are three ways to develop ASP.NET Server Controls.

Render Contents

You can override RenderContents and using the writer can flush out the HTML text you want. Its more or less the same Classic ASP like spaghetti approach. To make things bit object oriented there are some base methods that you can override. In the example below; I am trying to write out <a href=”http://weblogs.com.pk”>Weblogs.com.pk Link</a>

[ToolboxData(@"<{0}:RenderContentsControl runat=""server"" />")]
public class RenderContentsControl : WebControl
    protected override HtmlTextWriterTag TagKey
            return HtmlTextWriterTag.A;
    protected override void AddAttributesToRender(HtmlTextWriter writer)
        writer.AddAttribute(HtmlTextWriterAttribute.Href, "http://weblogs.com.pk");
    protected override void RenderContents(HtmlTextWriter writer)
        writer.Write("Weblogs.com.pk Link");

Inherited Control

The second option is you find the most suitable control and inherit your control from it adding the missing functionality from your perspective. You can inherit from as simple as Label control to as complex as TreeView control. In the example below; I have inherited my control from Panel giving it red borders as default look.

[ToolboxData(@"<{0}:PanelInheritedControl runat=""server""></{0}:PanelInheritedControl>")]
public class PanelInheritedControl :Panel
    public PanelInheritedControl()
        this.BorderStyle = BorderStyle.Solid;
        this.BorderColor = Color.Red;

Composite Control

The third option is that you override OnInit and add one or more controls into the control’ Controls collection. In the example below I have added couple of controls and one child control even has post back event!

[ToolboxData(@"<{0}:CompositeControl runat=""server"" />")]
public class CompositeControl : WebControl
    protected override void OnInit(EventArgs e)
        LinkButton lb = new LinkButton();
        lb.Text = "Weblogs.com.pk Link";
        lb.Click += new EventHandler(lb_Click);
        this.Controls.Add(new LiteralControl("<br />"));
        HtmlAnchor anchor = new HtmlAnchor();
        anchor.HRef = "mailto:khurram@sharpcoders.net";
        anchor.InnerText = "Mail Link to Khurram";
    void lb_Click(object sender, EventArgs e)

The ASPX hosting these three controls will look like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWebApplication._Default" %>
<%@ Register Assembly="Weblogs.Web" Namespace="Weblogs.Web" TagPrefix="weblogs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <weblogs:PanelInheritedControl ID="PanelInheritedControl1" runat="server">
            <weblogs:RenderContentsControl ID="RenderContentsControl1" runat="server" />
            <br />
            <weblogs:CompositeControl ID="CompositeControl1" runat="server" />

I am also attaching the Visual Studio 2008 Solution of the above samples with this post.

Do you use any other technique of developing Server Controls, please share with us!

Published Friday, July 3, 2009 1:49 AM by khurram
Filed under:

Attachment(s): Weblogs.zip


No Comments

New Comments to this post are disabled