Let ASPX pages control the number of call records, type, change at any time, do not recompile the implementation of the method _ practical skills

Source: Internet
Author: User
However, how to kill a chicken with a sledgehammer, for small sites, we can completely from the. NET powerful features, some to facilitate our use.
We know that a page will often call a lot of data, there are lists, there is a single, especially the list, we often for different lists in the. cs file to write a different method, using the list. Datasource= the data source, the list. DataBind () to complete the binding, which makes our CS page yong-swollen!!! I often have to push back the impulse, but again and again, I write a pile of garbage-_-!. OK, this time I decided to write only one method, less than 15 lines of method, to meet the entire page of the article call.

We need to use the common sense of "late binding", "run the CS again aspx", I believe we have already. We're going to get back to it:
First step: Define a protected method in CS:
Copy Code code as follows:

<summary>
Get a list of news data
</summary>
<param name= "num" > Request quantity </param>
<param name= "Cateid" > Category id</param>
<param name= "Istop" > is top </param>
<param name= "Iscommend" > Recommended </param>
<returns> a news data set, news is press, of course,list<xxxx> name depends on your data source </returns>
protected list<news> getnewsdata (int num,int cateid,int istop,int) {
if (num < 0) num = 0;
list<news> result = new list<news> ();
Expression<func<news, bool>> expr = predicateextensionses.true<news> ();
if (ispic) {
expr = expr. and (c => c.ispic = 1);
}
if (Cateid > 0) {
expr = expr. and (c => C.cateid = = Cateid);
}
if (Istop >-1) {
expr = expr. and (c => c.istop = = istop);
}
if (Iscommend >-1) {
expr = expr. and (c => c.iscommend = = iscommend);
}
return nm. Allnews.where (expr). OrderByDescending (c=>c.id). Take (num). ToList ();
}

Step two: In the Page_Load method, call DataBind ();
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
DataBind ();
}

These are all of my methods, and here's a common class that calls into the business logic to create an expression tree, which is here (http://www.jb51.net/article/28043.htm), and the generic business logic class;
The third step, then, is our aspx call. Here I use the simplest repeater to explain!
Copy Code code as follows:

<asp:repeater id= "REPEATER6" runat= "server" Datasource= ' <% #GetNewsData (10,3548,-1,-1)%> ' >
<ItemTemplate>
<% #Eval ("Subject")%>
</ItemTemplate>
</asp:Repeater>

All right, all work is over. To call other classes later, or to change the number, just change on the page, no need to change. CS and compile!
Isn't it convenient?
For instructions on DataBind (), why write the DataBind in Page_Load (), which is to give data to the source in advance! Otherwise, because the late-bound relationship will cause the data control to not find the data source and error!

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.