Saturday, June 21, 2008

Creating Asp .Net Custom Controls

Custom Controls are reusable dlls.
Start out by creating a class library in C#. Add reference to System.Web. We are overriding the Render method of System.Web.UI.Control class and using HtmlTextWriterClass we are outputing Text to the browser.

using System.Web;
using System.Web.UI;

namespace HelloCustomCtl
{
 public class HelloCtl : Control 
 {
  protected override void Render(HtmlTextWriter writer)
  {
   writer.Write("Shalvin");
  }
 }
}

Build the project.

Testing the Custom Control

Start ASP .Net, preferably add a new tab to the toolbox. Right click the tab and select Choose Item. Click on the browse button navigate to the bin folder of previously created class library and select the dll.
Now the control will appear in the toolbox and you can use it in your project.

WebControl With Properties
Since WebControl class provides the properties, methods, and events that are common to all Web server controls, this control eventually will have a set of common properties found in common controls.
Along the way we are implementing two properties Text and Location that can be set from propeties window while using the control.

using System.Web.UI;
using System.Web.UI.WebControls;

public class MultiTdCtl : WebControl
{

private string mText;
public string Text
{
get { return mText ; }
set { mText = value; }
}

private string mLocation;
public string Location
{
get
{
return mLocation;
}
set
{
mLocation = value;
}
}

protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Border, "1");
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(mText); writer.RenderEndTag ();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.WriteLine(mLocation);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}

With C# 3.0 the lengthy getter and setter code is not required in fact you replace the code with the following :

public string Text
{
get;
set;
}
public string Location
{
get;
set;
}

..writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(Text); writer.RenderEndTag ();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.WriteLine(Location);

Creating Composite Control

using System.Web;
using System.Web.UI.WebControls;

namespace CompositeCtl
{
public class CompositeCtl : CompositeControl
{
    Label lbl;
    TextBox txt;

   protected override void CreateChildControls()
    {
        lbl = new Label { Text = "Name" };
        this.Controls.Add(lbl);


        txt = new TextBox();
        this.Controls.Add(txt);
    }
}
}
Composite Control 2
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CompositeCtlEg
{
   public class ShalvinCompositeCtl : CompositeControl
    {
       Label lbl, lblMessage;
       TextBox txt;
        Button btn;
        protected override void CreateChildControls()
        {
            lblMessage = new Label { Text = "" };
            this.Controls.Add(lblMessage);
            lbl = new Label { Text = "Name" };
            this.Controls.Add(lbl);

            txt = new TextBox();
            this.Controls.Add(txt);

            btn = new Button { Text = "Shalvin" };
            btn.Click +=new EventHandler(btn_Click);
            this.Controls.Add(btn);
        }

        void btn_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Hello " + txt.Text;
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
           
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
           lblMessage.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            lbl.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txt.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            btn.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            
            writer.RenderEndTag();
        }
    }
}

No comments:

Post a Comment