Can someone comment my Custom control?

F

Flare

Hi.

I have now used ASP.NET for a couple of weeks and it _really_ looks good.

I have made a custom control, wich has to act as my header on every site.
Before i go any further with my control I would appreciate your comments on
the design (wich problably is bad) Sorry for the long post...

Eg. is the RenderBeginTag and the RenderEndTag() approch ok?

[DefaultProperty("Text"),
ToolboxData( said:
public class ShowHeaderControl : System.Web.UI.WebControls.WebControl
{
private string displayedUserName = "Unknown";
private string headerText;

[Bindable(true),Category("Appearance"), DefaultValue("unknown")]
public string DisplayedUserName
{
get { return displayedUserName; }
set { displayedUserName = value; }
}

Style _style = new Style();

[Bindable(true),Category("Farver"), DefaultValue("")]
public Style LabelStyle
{
get { return _style; }
}

[Bindable(true),Category("Farver"), DefaultValue("")]
public Color ForeColor
{
get { return _style.ForeColor; }
set { _style.ForeColor = value; }
}

protected override void Render(HtmlTextWriter html)
{
_style.AddAttributesToRender( html );

html.AddAttribute("id","TableHeader");
html.AddAttribute("width","100%");
html.AddAttribute("class","test");
html.RenderBeginTag("table");

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
html.Write(headerText);
html.RenderEndTag();

html.RenderBeginTag("TD");
html.RenderEndTag();

html.RenderEndTag();

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
html.RenderEndTag();
html.RenderBeginTag("TD");

html.Write("Bruger: ");
html.Write(displayedUserName);
html.RenderEndTag();

html.RenderEndTag();

}
}
(xpostet in microsoft.public.dotnet.framework.aspnet.buildingcontrols,
microsoft.public.dotnet.framework.aspnet.webcontrols)
Regards
Anders
 
T

Teemu Keiski

Hi,

I bit rewrote your control. Comments and some explanation is provided in
code comments what has been done and why. If you have question about
implementation, go ahead and ask.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

[DefaultProperty("Text"),
ToolboxData("<{0}:ShowHeaderControl
runat=server></{0}:ShowHeaderControl>")]
public class ShowHeaderControl : System.Web.UI.WebControls.WebControl
{
//Changed to use ViewState so that value is preserved over postbacks
//Important if value can be set programmatically as then it is not set for
every request
//When control instance is created (like it is when specified in
declarative syntax)
[Bindable(true),Category("Appearance"), DefaultValue("unknown")]
public string DisplayedUserName
{
get {
string s=(string)ViewState["DisplayedUserName"];
return(s==null)? "unknown" : s;
}
set {
ViewState["DisplayedUserName"]=value;
}
}

//Created property for HeaderText as well
[Bindable(true),Category("Appearance"), DefaultValue("unknown")]
public string HeaderText
{
get
{
string s=(string)ViewState["HeaderText"];
return(s==null)? "unknown" : s;
}
set
{
ViewState["HeaderText"]=value;
}
}

//Removed Style declaration completely
//This control inherits from WebControl and it therefore has built-in style
support
//via it's ControlStyle property

//Changed the property to override base class's implementation
//as WebControl already has ForeColor property
[Bindable(true),Category("Farver"), DefaultValue("")]
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor =value;
}
}



protected override void Render(HtmlTextWriter html)
{
ControlStyle.AddAttributesToRender( html );
html.AddAttribute("id","TableHeader");
html.AddAttribute("width","100%");
html.AddAttribute("class","test");
html.RenderBeginTag("table");

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
//Changed to use property
html.Write(HeaderText);
html.RenderEndTag();

html.RenderBeginTag("TD");
html.RenderEndTag();

html.RenderEndTag();

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
html.RenderEndTag();
html.RenderBeginTag("TD");

html.Write("Bruger: ");
//Changed to use property
html.Write(DisplayedUserName);
html.RenderEndTag();

html.RenderEndTag();

}
}

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com

Flare said:
Hi.

I have now used ASP.NET for a couple of weeks and it _really_ looks good.

I have made a custom control, wich has to act as my header on every site.
Before i go any further with my control I would appreciate your comments on
the design (wich problably is bad) Sorry for the long post...

Eg. is the RenderBeginTag and the RenderEndTag() approch ok?

[DefaultProperty("Text"),
ToolboxData("<{0}:ShowHeaderControl runat=server></{0}:ShowHeaderControl
public class ShowHeaderControl : System.Web.UI.WebControls.WebControl
{
private string displayedUserName = "Unknown";
private string headerText;

[Bindable(true),Category("Appearance"), DefaultValue("unknown")]
public string DisplayedUserName
{
get { return displayedUserName; }
set { displayedUserName = value; }
}

Style _style = new Style();

[Bindable(true),Category("Farver"), DefaultValue("")]
public Style LabelStyle
{
get { return _style; }
}

[Bindable(true),Category("Farver"), DefaultValue("")]
public Color ForeColor
{
get { return _style.ForeColor; }
set { _style.ForeColor = value; }
}

protected override void Render(HtmlTextWriter html)
{
_style.AddAttributesToRender( html );

html.AddAttribute("id","TableHeader");
html.AddAttribute("width","100%");
html.AddAttribute("class","test");
html.RenderBeginTag("table");

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
html.Write(headerText);
html.RenderEndTag();

html.RenderBeginTag("TD");
html.RenderEndTag();

html.RenderEndTag();

html.RenderBeginTag("TR");
html.RenderBeginTag("TD");
html.RenderEndTag();
html.RenderBeginTag("TD");

html.Write("Bruger: ");
html.Write(displayedUserName);
html.RenderEndTag();

html.RenderEndTag();

}
}
(xpostet in microsoft.public.dotnet.framework.aspnet.buildingcontrols,
microsoft.public.dotnet.framework.aspnet.webcontrols)
Regards
Anders
 
F

Flare

thx for using your time Teemu!

The viewstate issue explains alot of my problems. So great.! Didnt know the
style was inhirited. But the RenderBeginTag approach is ok? It just seems a
little ....cumbersome, and what is the diffrence between WriteBeginTag and
RenderBeginTag besides your have to explicit write the end tag with
WriteEndTag when using WriteBeginTag?

Regards
Anders
 
T

Teemu Keiski

The difference is actually bit difficult to explain simply. Docs say this
about them:

WriteBeginTag
************
This method does not write the closing character (>) of the HTML element's
opening tag. This allows the writing of HTML attributes to the opening tag
of the element. Use the TagRightChar constant to close the opening tag when
calling this method. Use this method with the SelfClosingTagEnd constant
when you write HTML elements that are self closing.
This method is used by custom server controls that do not allow tag or
attribute mapping and render HTML elements in the same way for each request.

RenderBeginTag (string)
*************
Use this version of the RenderBeginTag method if the tag is not one of the
HtmlTextWriterTag enumeration value. This version should not be overridden.
A custom server control that calls this method must first call the
AddAttribute and AddStyleAttribute methods. It then calls this method,
passing the identifier for its HTML element. Finally, the control calls the
RenderEndTag method.
Note Calls to this method must be paired with call to the RenderEndTag
method.

RenderBegintag (HtmlTextWriterTag)
*****************************
Use this version of the RenderBeginTag method if the HTML element is of a
known type. This is also the version that you should override when you
inherit from the HtmlTextWriter class.
A custom server control that calls this method must first call the
AddAttribute and AddStyleAttribute methods. It then calls this method,
passing the identifier for its HTML element. Finally, the control calls the
RenderEndTag method.
Note Calls to this method must be paired with a call to the RenderEndTag
method

What I'd say is that I'd use RenderBeginTag because of the simplicity and
adaptive rendering capability it brings (with enumerations) and it is also
simply recommended in the book "Developing ASP.NEt Server Controls and
Components" by MSPress (it's the book about controls)

What comes to using RenderBeginTag is that rendering of server controls uses
normally more than one method to form the output. With WebControls the
implementation is:

protected override void Render(HtmlTextWriter writer)
{
RenderBeginTag(writer);
RenderContents(writer);
RenderEndTag(writer);
}

public virtual void RenderBeginTag(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
HtmlTextWriterTag tagKey=TagKey;
if(tagKey != HtmlTextWriterTag.Unknown)
{
writer.RenderBeginTag(tagkey);
}
else
{
writer.RenderBeginTag(this.TagName);
}
}

protected virtual void RenderContents(HtmlTextWriter writer)
{
//Calls Control class's method (calls again RenderChildren(writer) that
renders child controls)
base.Render(writer);
}

As you can see (sorry if there are small typos), like you did, overriding
the Render and not calling base class methods removes much of ready-made
functionality of WebControl. For example: you don't have to use
RenderBeginTag manually to set the tag, but you could just set the tag to
TagKey property of controls and for contents just override RenderContents
method and you don't have need to override the Render method. Or if you want
to do it in render, just call base.AddAttributesToRender(writer) and
base.RenderBeginTag(writer) to get them to come automatically from
WebControls properties (still uses tagKey property).

Hope this helped and gave something to think. ;-)

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top