Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [274]

By Root 5771 0
an HTML text writer object is the preferred approach. No server control is ever instantiated, and the final and correct markup is sent to the browser. There are a few downsides to this approach you should consider, however. One is that you end up making several calls to the writer. And, aside from some negligible repercussions in terms of the performance (repercussions that are negligible when compared to control instantiation), the size of the code grows considerably, making your source code on the whole much less readable and harder to maintain. Let’s consider a quick but significant example.

To write the content of a string in a table cell, you need the following code if you decide to opt for the rich interface of the writer:

output.WriteFullBeginTag("table");

output.WriteFullBeginTag("tr");

output.WriteFullBeginTag("td");

output.Write(text);

output.WriteEndTag("td");

output.WriteEndTag("tr");

output.WriteEndTag("table");

However, as long as you don’t have a full bag of attributes to render, or a really complex structure to build, the following code is equally effective and even slightly faster:

output.Write("
");

output.Write(text);

output.Write("

");

In general, neither of these two approaches is always the best possible approach. A good compromise between the two is recommended to optimize performance while producing compact code. Taking the first approach to its natural limit, you end up with many more lines of code than are necessary. Taking the second approach further, you resort to building the control using strings, which is indeed not the best thing you can do, mainly from a maintenance point of view.

In ASP.NET, every piece of HTML code can be managed on the server as an instance of a class. This pattern results in extreme flexibility and ease of development. However, it doesn’t come without problems either. The rub lies in the fact that you instantiate lots of controls, which always affects performance. Let’s take a look at this in more detail.

Using Child Controls for Rendering


Sometimes the custom control needs to build up a complex infrastructure with nested tables and elements. In this case, it makes sense to build an in-memory representation of the overall tree and then render everything to HTML using the RenderContents method of the root control. Typically, for controls with a relatively complex hierarchy of child controls and rich styles, you override the Render method as follows:

protected override void Render(HtmlTextWriter output)

{

// This is a custom method that you normally use

// to ensure that all elements are styled properly.

// We'll show an implementation of this method later.

PrepareControlForRendering();

// Render the contents of the control

base.RenderContents(output);

}

The SimpleGaugeBar control renders a nontrivial table structure that is much more manageable through a control tree:

protected override void CreateChildControls()

{

Controls.Clear();

CreateControlHierarchy();

ClearChildViewState();

}

protected virtual void CreateControlHierarchy()

{

// Build the outermost container table

var outer = new Table();

var outerRow = new TableRow();

outer.Rows.Add(outerRow);

// Ruler cell

var rulerCell = new TableCell();

outerRow.Cells.Add(rulerCell);

BuildGaugeBar(rulerCell);

// Text cell

var textCell = new TableCell();

if (!_textStyle.DisplayTextAtBottom)

{

outerRow.Cells.Add(textCell);

BuildLabel(textCell);

}

// Save the control tree-add the table as a child of the gauge

Controls.Add(outer);

// Build the label

if (!_textStyle.RenderInsideTable && _textStyle.DisplayTextAtBottom)

BuildLabel(null);

}

void BuildGaugeBar(TableCell container)

{

// Create the table with one or two rows: ruler (and label)

var t = new Table();

var ruler = new TableRow();

t.Rows.Add(ruler);

// Build the ruler row

BuildRuler(ruler);

// Build the label

if (_textStyle.RenderInsideTable)

BuildLabelIntoTable(t);

// Save the control tree

container.Controls.Add(t);

}

The output of the SimpleGaugeBar

Return Main Page Previous Page Next Page

®Online Book Reader