However, how can we use a knives to kill chickens? For small sites, we can find some of the powerful functions of .net for us to use.
We know that a page often calls a lot of data, including lists, single ones, especially lists. We often write different methods for different lists in .cs files, using "list.DataSource=data source; list.DataBind(); to complete the binding, which makes our CS page swell!!! I often have the urge to push it back, but over and over again, I wrote a bunch of sluts-_-! Well, this time I decided to only write one method, and less than 15 lines of method can satisfy the call of the entire page article.
The common sense we need to use is "late binding", "run CS first and then aspx", I believe everyone already has it. Let's get back to the point:
Step 1: First define a protected method in cs:
/// <summary>
/// Get a list of news data
/// </summary>
/// <param name="num">Request count</param>
/// <param name="cateId">Category ID</param>
/// <param name="isTop">Whether to top</param>
/// <param name="isCommend">Is it recommended</param>
/// <returns>A news dataset, 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 = <News>();
if (isPic) {
expr = (c => == 1);
}
if (cateId > 0) {
expr = (c => == cateId);
}
if (isTop > -1) {
expr = (c => == isTop);
}
if (isCommend > -1) {
expr = (c => == isCommend);
}
return (expr).OrderByDescending(c=>).Take(num).ToList();
}
Step 2: In the Page_Load method, call DataBind();
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
The above is all my methods. What I want to say here is to call a public class in business logic that creates an expression tree. This class is here (https:///article/), and general business logic;
The third step is our aspx call. Here I will use the simplest Repeater to explain it!
<asp:Repeater ID="Repeater6" runat="server" DataSource='<%#GetNewsData(10,3548,-1,-1)%>'>
<ItemTemplate>
<%#Eval("Subject")%>
</ItemTemplate>
</asp:Repeater>
OK, all work is over. In the future, if other categories are called, or the quantity needs to be changed, you only need to change on the page, and there is no need to change.cs and compilation!
Isn't it quite convenient?
Regarding DataBind(), why do you need to write DataBind() in Page_load? This is to give data to the data source in advance! Otherwise, due to the later binding relationship, the data control will not be able to find the data source and an error will be reported!
We know that a page often calls a lot of data, including lists, single ones, especially lists. We often write different methods for different lists in .cs files, using "list.DataSource=data source; list.DataBind(); to complete the binding, which makes our CS page swell!!! I often have the urge to push it back, but over and over again, I wrote a bunch of sluts-_-! Well, this time I decided to only write one method, and less than 15 lines of method can satisfy the call of the entire page article.
The common sense we need to use is "late binding", "run CS first and then aspx", I believe everyone already has it. Let's get back to the point:
Step 1: First define a protected method in cs:
Copy the codeThe code is as follows:
/// <summary>
/// Get a list of news data
/// </summary>
/// <param name="num">Request count</param>
/// <param name="cateId">Category ID</param>
/// <param name="isTop">Whether to top</param>
/// <param name="isCommend">Is it recommended</param>
/// <returns>A news dataset, 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 = <News>();
if (isPic) {
expr = (c => == 1);
}
if (cateId > 0) {
expr = (c => == cateId);
}
if (isTop > -1) {
expr = (c => == isTop);
}
if (isCommend > -1) {
expr = (c => == isCommend);
}
return (expr).OrderByDescending(c=>).Take(num).ToList();
}
Step 2: In the Page_Load method, call DataBind();
Copy the codeThe code is as follows:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
The above is all my methods. What I want to say here is to call a public class in business logic that creates an expression tree. This class is here (https:///article/), and general business logic;
The third step is our aspx call. Here I will use the simplest Repeater to explain it!
Copy the codeThe code is as follows:
<asp:Repeater ID="Repeater6" runat="server" DataSource='<%#GetNewsData(10,3548,-1,-1)%>'>
<ItemTemplate>
<%#Eval("Subject")%>
</ItemTemplate>
</asp:Repeater>
OK, all work is over. In the future, if other categories are called, or the quantity needs to be changed, you only need to change on the page, and there is no need to change.cs and compilation!
Isn't it quite convenient?
Regarding DataBind(), why do you need to write DataBind() in Page_load? This is to give data to the data source in advance! Otherwise, due to the later binding relationship, the data control will not be able to find the data source and an error will be reported!