We often make some enterprise sites. For data calling, we have a headache and try our best to make data display simple and easy to maintain, this allows us to create templates and other things to engage in enterprise websites, portals, etc,
However, for small websites, we can find some powerful. NET functions to facilitate our use.
We know that a page often calls a lot of data, including lists and single items, especially lists. We often find different lists in. different Methods for writing CS files use "list. datasource = data source; List. databind (); to complete the binding, which makes our CS page very swollen !!! I often have the urge to roll back, but I wrote a bunch of pull -_-!. Well, this time I decided to write only one method. Less than 15 rows of methods will satisfy the call of the entire page article.
The common knowledge we need is "binding later" and "Running CS first and then aspx". I believe everyone has the knowledge. Let's get down to it:
Step 1: first define a protected method in Cs:
/// <Summary>
/// Obtain the news data list
/// </Summary>
/// <Param name = "num"> Number of requests </param>
/// <Param name = "cateid"> category id </param>
/// <Param name = "Istop"> top? </param>
/// <Param name = "iscommend"> recommended or not </param>
/// <Returns> A news dataset where news is news. Of course, the list <XXXX> name depends on your data source </returns>
Protected list <News> getnewsdata (INT num, int cateid, int Istop, int iscommend ){
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 2: Call databind () in the page_load method ();
Protected void page_load (Object sender, eventargs E)
{
Databind ();
}
The above is all of my methods, here to talk about is to call a business logic in the creation of Expression Tree public class, this class here (http://www.cnblogs.com/CoreCaiNiao/archive/2010/08/25/1808284.html), and general business logic class;
Step 3: Our aspx is called. Here I will describe it with the simplest repeater!
<Asp: repeater id = "repeater6" runat = "server" datasource = '<% # getnewsdata (10, 3548,-1,-1) %>'>
<Itemtemplate>
<% # Eval ("subject") %>
</Itemtemplate>
</ASP: repeater>
All right, after all the work is finished, you only need to modify it on the page to call other categories or change the quantity. No need to change. CS or compile it!
Is it convenient?
For the description of databind (), why do we need to write databind () in page_load? This is to give data to the data source in advance! Otherwise, an error will be reported if the data control cannot find the data source due to the later binding relationship!