In WebForm display list data we generally use server controls Repeater, DataList, or GridView, powerful and simple to use. But at the same time there is a price,
One: no matter which control you use, you need to sacrifice some extra performance because their life cycle is complex.
Second: The binding data is not flexible, the process is opaque, it is troublesome to want to take extra control of a line at will.
Three: A pair of "junk code" will be generated to increase the burden of page generation and display.
Four: The control of the CSS style is not flexible, if it is teamwork, and art communication a little trouble, art design of static page developers can not even directly to use
So discard these controls, write code directly to generate a list of data can solve all the above problems, this is the most transparent, the most flexible, the cleanest display, then we do it.
How do you do it? Very simple, really simple, cow mo look.
Just raise the method or variable in the *.aspx.cs on the page *.aspx and then loop the output.
For example, we want to display a list of user information data on the page.
First we see a model class, as a demo we will not build a database, as follows:
PersonOM.cs
public class Personom
{
public string Name {get; set;}
public int Age {get; set;}
}
Just two attributes, name and age.
Then write a method in WebForm1.aspx.cs to get the user information:
WebForm1.aspx.cs:
public partial class WebForm1:System.Web.UI.Page
{
public static list<personom> GetResult ()//I am writing a static method here so that you do not need to instantiate the current page class when you call it
{
list<personom> Lstps = new list<personom> ();
Personom PS = null;
for (int i = 1; i <=; i++)
{
PS = new Personom ();
Ps. Name = "name" + i;
Ps. Age = + I;
Lstps.add (PS);
}
return LSTPS;
}
}
Then there is the binding data, and here we no longer use the server control, we loop the output on the WebForm1.aspx page:
<ul>
<% list<personom> Lstps = Webform1.getresult (); %>
<% foreach (personom PS in Lstps)
{%>
<li> Name: <%=ps. Name%> Age: <%=ps. Age%></li>
<%}%>
</ul>
In the browser browsing, we see the following screen:
This generates a page that does not contain any junk code.
If you've ever used ASP. NET MVC, you'll find it very familiar with this MVC. Actually this is also the MVC idea, has the model (PersonOM.cs), has the controller (WebForm1.aspx.cs), has the view (WebForm1.aspx), but has realized the separation between the three.
Of course, the GetResult () method does not necessarily have to be written as static, not static, but it is also possible to instantiate the current page class when it is output, and must be in <form runat= "Server" >.
That
<form id= "Form1" runat= "Server" >
<% WebForm1 wf1 = new WebForm1 (); %>
<ul>
<% list<personom> Lstps = Webform1.getresult (); %>
<% foreach (personom PS in Lstps)
{%>
<li> Name: <%=ps. Name%> Age: <%=ps. Age%></li>
<%}%>
</ul>
</form>
In fact, back to think, WebForm1 is a partial (partial) class, part in the WebForm1.aspx.cs, part in the WebForm1.aspx, aware of this, on the WebForm1.aspx page of the relevant operation is easy to understand.
SOURCE download
Get rid of Datalist,repeater, the list data show flexible--turn