Get rid of datalist and repeater. The list data is displayed flexibly.

Source: Internet
Author: User
To display list data in webform, we generally use server controls such as repeater, datalist, or gridview, which are powerful and easy to use. But it also has a price,
I. No matter which control you use, you need to sacrifice some extra performance because their lifecycle is complex.
2. The data binding process is not flexible enough. The display process is not transparent. It is troublesome to control a row as you like.
3: generate a pair of "spam" Code "To increase the burden of page generation and display.
Iv. Lack of flexibility in controlling CSS styles. If it is a matter of teamwork, it is a bit difficult to communicate with the artist. The static page developers designed by the artist cannot even use them directly.
Therefore, dropping these controls and writing code to generate a data list can solve all the above problems. This is also the most transparent, flexible, and clean display method. Let's do this.
How can this problem be solved? Very simple. It's really very easy.
You only need to call methods or variables in *. aspx. CS on the page *. aspx, and then output them cyclically.

For example, we want to display a list of user information data on the page.

First, we will see a model class. As a demo, we will not create a database, as shown below:
Personom. CS

Public   Class Personom
{
Public   String Name { Get ; Set ;}

Public IntAge {Get;Set;}
}

There are 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 () // Here I am writing a static method, so that the current page class does not need to be instantiated when calling the page.
{
List < Personom > Lstps =   New List < Personom > ();
Personom PS =   Null ;
For ( Int I =   1 ; I <=   10 ; I ++ )
{
PS =   New Personom ();
PS. Name =   " Name "   + I;
PS. Age =   20   + I;
Lstps. Add (PS );
}
Return Lstps;
}

}

Then we bound the data. Here we no longer use the server control. We cyclically output the following on the webform1.aspx page:

< Ul >
<% List < Personom > Lstps = Webform1.getresult (); %>
<% Foreach (personom PS in lstps)
{ %>
< Li > Name: <% = PS. Name %> & Nbsp; Age: <% = PS. Age %> </ Li >
<% } %>
</ Ul >

In the browser, we can see the following picture:

The generated page does not contain any junk code.

If you have used Asp.net MVC, you will find that you are very familiar with this MVC. In fact, this is also the MVC idea. There are model (personom. CS), controller (webform1.aspx. CS), and view (webform1.aspx), and they all implement separation between the three.

Of course, you do not have to write the getresult () method as static, but not static. However, you must instantiate the current page class during page output, and must be in <form runat = "server">.
That is:   < Form ID = "Form1" Runat = "Server" >
<% Webform1 WF1 =   New Webform1 (); %>

< Ul >
<% List < Personom > Lstps = Webform1.getresult (); %>
<% Foreach (personom PS in lstps)
{ %>
< Li > Name: <% = PS. Name %> & Nbsp; Age: <% = PS. Age %> </ Li >
<% } %>
</ Ul >

</ Form >

In fact, looking back, webform1 is a part (partial) class, and some are in webform1.aspx. in CS, some of them are in webform1.aspx, and the operations on the webform1.aspx page are easy to understand.

Source code download

 

Address: http://www.cnblogs.com/xumingxiang/archive/2010/05/04/1727586.html

Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.