page 1 of 1
Comments: 8 | Views: 2103
JohnnyAwesome
JohnnyAwesome
Eggshell with Romalian type. What do you think?

This may be a rather rudimentary question, but I really have no concept of the design approach many ASP.NET developers take in terms of instancing and manipulating server controls. I mean, I have read my fair share of books; but in general I feel like frequently the code samples are either simplistic or simply taking a "traditional approach".

 

I feel at home in code-behind. If I am doing something gnarly (heavy client-side coding) on the front-side, I am more inclined to create a control, reference, instance it, and then do whatever I need to (maybe that's just a leftover 1.x habbit, I dunno). Just seems more reusable to me (granted I do suffer from that illness of 'what if I need this in the future; better have a reusable control for it').

 

Given this fact, when it comes to reaching out to the mark-up file I love to use placeholder, panel, and literals. Pretty much anything that allows me to add controls, I guess, depending upon the situation.

 

My question is two fold: 1.) Is there any real difference in performance or trade-offs of one approach over another? And 2.) Which do you prefer when working in ASP.NET?

 

 

I can't stand it when people use Label controls to populate their ASP.NET pages with markup, like this:

Label1.Text = "<a href=\"javascript:void(0)\">Link</a>";

Anytime you see someone using StringBuilders to generate lines upon lines of HTML, you just know that Angel it's procedural, not object-oriented code and (b) it's going to be unmaintainable spaghetti code after they're done.  They are PHP/ASP programmers in ASP.NET programmers' clothing.

Use the intrinsic HtmlControls and WebControls to generate your markup:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="javascript:void(0)" Text="Link" />

or

<a id="Anchor1" runat="server" href="javascript:void(0)">Link</a>

A suitable but more advanced alternative would be to learn how to write your own server-side controls.  Here is a crude example, but the gist of the suggestion here is that you want to make reusable code that Angel has a semantically useful presence in your ASP.NET markup and (b) provides some sort of Visual Studio/Expression Studio design-time experience.

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

namespace MyControlLib
{
[DefaultProperty("Text")]
public class MyAnchor : System.Web.UI.Control
{
public MyAnchor()
: base()
{
Href = "javascript:void(0)";
Text = "Link";
}

[DefaultValue("javascript:void(0)")]
public string Href
{
get { return (string)ViewState["href"] ?? string.Empty; }
set { ViewState["href"] = value; }
}

[DefaultValue("Link")]
public string Text
{
get { return (string)ViewState["Text"] ?? string.Empty; }
set { ViewState["Text"] = value; }
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);

bool inDebugMode = HttpContext.Current == null;

writer.WriteBeginTag("A");
writer.WriteAttribute("href", Href, true);
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(string.IsNullOrEmpty(Text) && inDebugMode) ? string.Format("[{0}]", ID) : Text);
writer.WriteEndTag("A");
}
}
}

In the markup, it would look like this:

<%@ Register Assembly="MyControlLib" Namespace="MyControlLib" TagPrefix="cc" %>
<cc:MyAnchor ID="MyAnchor1" runat="server" />

Using StringBuilders is the semantic equivalent of using document.writeln()-style JavaScript to build Web pages. It's an unreadable, unmaintainable mess where your ideas are expressed with crude, long-winded, ill-suited metaphors rather than clear, concise language.

UserControls are another great way to develop your own custom server-side controls without having to "live in the code-behind."

Well, from what I have read, the markups are actually template for creating the 'real' classes. So, after the first run, it will get parsed and then converted to real classes, and then get compiled to assemblies. whenever there is a request, ASP.Net will create the appropriate class from the resulting assembly. So, the time penalty is only for the first time load. Furthermore, ASP.Net cached these assemblies. Regarding the performance, there's no difference in the two versions except for the first time load. BTW, you can check the resulting classes and assemblies somewhere in

/Windows/Microsoft.Net/Framework/<.net framework version you're using>/Temporary ASP.Net Files/<projectname>/<drill down from here>

hope that helps
JChung2006 wrote:


Use the intrinsic HtmlControls and WebControls to generate your markup:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="javascript:void(0)" Text="Link" />

or

<a id="Anchor1" runat="server" href="javascript:void(0)">Link</a>

A suitable but more advanced alternative would be to learn how to write your own server-side controls. 


I don't know, but I heard the HtmlControls.HtmlAnchor and WebControls.HyperLink crying for being neglected. [C]

Those classes can be easily extended by using <Control>.Attributes.Add() for any new properties. There's little need to write your own UserControl to replace them as Validation/Customization for <a> tags are rarely needed.
cheong wrote:
Those classes can be easily extended by using <Control>.Attributes.Add() for any new properties. There's little need to write your own UserControl to replace them as Validation/Customization for <a> tags are rarely needed.

Yeah, I picked a really bad example to demonstrate writing your own server-side control.  A better example might have been one that wrapped one of that nifty JavaScript controls like the ones in YUI or ext-js so that people could use them as server controls in their ASP.NET projects.  The code for that, needless to say, would have been way too long for this post. Wink
JohnnyAwesome wrote:
Oh and JChung, I don't think .NEt newbies would even use StringBuilder. It would much more like string s = "";

Wink

Ha!  Yes, you're so right about that.