Custom Controls

ASP.NET makes custom controls extremely easy to write, but if you are not careful, hard to manage.

There are 2 ways to create a custom control

  1. Writing your own HTML code
  2. Using the controls property

Writing your own HTML code

Using your own properties, you can easily write HTML code to generate the
output

e.g consider a simple scenario where you take in a text and color input and
render the control.

You can simply create a class and extend it from the WebControl base class to
have the functions available to your custom control.
Thereafter, just create the properties you require and use them inside the
Render function.
Do note that you can render different output when in design mode and actual
rendering, all you need is to check the DesignMode property.
If however you need input boxes, do take note that you need to handle the
postback event and put the value back into the inputs manually.

public class
SimpleControl :
WebControl

{

public
SimpleControl()

{

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string Text

{

get

{

object obj =
this.ViewState[“Text”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Text”]
=
value; }

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string Color

{

get

{

object obj =
this.ViewState[“Color”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Color”]
=
value; }

}

protected
override
void Render(HtmlTextWriter
writer)

{

writer.Write(“<font color='”
+ Color +
“‘>” +
Text +
“</font>”);

//base.Render(writer);

}

}

Using the controls property

The good thing about using the controls property is that you do not need to
care about ViewState (it is handled by the individual control), Postback
inputs handling (handled again by the individual control).

The bad thing is

  1. Have to write your own custom event handlers
  2. Everything needs to be control based, you cannot emit your own html.

public class
LabelExtendedControl
:
Label

{

public
SimpleControl()

{

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string xText

{

get

{

object obj =
this.ViewState[“Text”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Text”]
=
value; }

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string xColor

{

get

{

object obj =
this.ViewState[“Color”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Color”]
=
value; }

}

protected
override
void Render(HtmlTextWriter
writer)

{

this.Text = xText;

this.ForeColor =
System.Drawing.
Color.FromKnownColor(xColor);

base.Render(writer);

}

}

Custom Events

To create your own custom events, you need
to write 3 main items

  1. A readonly object containg the event
  2. A function which will call the Event
  3. The actual event handler itself

private
readonly
object EventCreate =
new
object();

protected
virtual
void OnCreate(EventArgs
e)

{

EventHandler
onCreateHandler = (
EventHandler)Events[EventCreate];

if
(onCreateHandler !=
null)
onCreateHandler(
this,
e);

}

[Category(“Action”),Description(“Raised
when a CommandEvent occurs within an item.”
)]

public
event
EventHandler Create

{

add

{

Events.AddHandler(EventCreate, value);

}

remove

{

Events.RemoveHandler(EventCreate, value);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *