ASP.NET Web Forms Weaknesses - Code Explained
(Page 3 of 5 )
As you can see above, GridView generates a <table /> element with many ugly items in it. It's pretty difficult to control the style of the table. Although GridView provides detailed style definition from the title to each cell, there is far less flexibility. And also, the GridView control-generated HTML tags do not correspond to Web standards (e.g. cannot generate the <th/> tag).
So, in this sense, we can complain about Web Forms, while at the same time we advocate rejecting the GridView-like complex controls (including DataList, FormView, etc.). Then a question arises: when GridView is rejected, what's the sense of still using Web Forms?
In fact, the ViewState, Postback, and GridView are not all of Web Forms. In my opinion, the Control model (or the Component model) really represents the key part of Web Forms. As you may know, the infrastructure of this model is absolutely excellent. Now I will show you some related examples (although they are very elementary).
First, let's take a look at the commonly-used DemoControl.ascx user control:
<%@ Control Language="C#" AutoEventWireup="true" %>
<%= "Hello World!" %>
Then, we put it inside an .aspx page:
<div>
<uc1:DemoControl ID="DemoControl1" runat="server" />
</div>
When you launch the above page in a browser and view the related page source, you will get the following:
<div>
Hello World!
</div>
How clean the above code is!
Another example is Master Page: its <asp:ContentPlaceHolder /> will not generate any redundant code. This proves an important fact that a web form based upon user controls will not produce useless and nasty code. Besides this, most of the fundamental controls, such as TextBox, CheckBox (not setting Text property), Panel, etc. will not produce useless code at all.
So, where on earth did the above-mentioned "nasty" tags come from? As you may have guessed, they all resulted from complex server controls, such as GridView, FormView, and many Data controls. However, there is an exception among these complex controls-Repeater. The following gives an example of the Repeater control:
<asp:Repeater runat="server" ID="rptItems">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<img src="<%# Eval("ImagePath")) %>" alt="<%# Eval("Title") %>" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
When you watch the resulting HTML tags rendered on the browser side (herein we omit it), you will find there is also clean code generated for the Repeater control without even a line of useless code.
With the release of ASP.NET 3.5, a new and powerful server control comes to you-ListView. Nowadays, you can easily find many examples related to this control, so we won't dwell on it here. However, there is an important point that deserves to be emphasized: as with the Repeater control, ListView will lead to clean HTML marks, and you can manipulate its HTML contents all by yourself.
To try to avoid using the GridView, DataList, and FormView. I highly recommend that you use Repeater and ListView as many as possible.
Next: The JavaScript Weakness >>
More ASP.NET Articles
More By Xianzhong Zhu