Intimate contact asp.net (13) to achieve personalized paging

Source: Internet
Author: User
Tags prev

Personalized pagination to achieve our previous page, but by modifying the properties of the DataGrid to achieve pagination, so that there is such a benefit, the biggest is simple, oh, do not worry about how pagination is produced.

But it is also flawed and does not produce the kinds of styles we want to do as we imagine.

There is no way to personalize the function, only to do it yourself, hehe.

The first step is to import the required namespaces. The following example, in fact, I also found from abroad, plus some personal things, and then Chinese, hehe. Today the mood is very good, I even the label color has shown to everybody. Oh, more conducive to everyone to see the program.

<%@ Page language= "C #"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>

Let's take a look at what our web controls are, and then look at how the code is written, which is better:

<body>

<form runat=server>

<asp:datagrid id= "Mydatagrid" runat= "Server"
Allowpaging= "True"
Pagesize= "10"
Pagerstyle-mode= "NumericPages"
Pagerstyle-horizontalalign= "Right"
Onpageindexchanged= "Mydatagrid_page"
Bordercolor= "BLACK"
Borderwidth= "1"
Gridlines= "Both"
Cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "#eeeeee"
Autogeneratecolumns= "false"

>
<property name= "Columns" >
<asp:boundcolumn headertext= "Studio member" datafield= "enter_id"/>
<asp:boundcolumn headertext= "Landing Time" datafield= "Enter_time"/>
</property>
</ASP:DataGrid>

<p>
<asp:linkbutton id= "Btnfirst" runat= "Server"
text= "Home"
commandargument= "0"
Forecolor= "Navy"
Font-name= "Verdana" font-size= "8pt"

/>

<asp:linkbutton id= "Btnprev" runat= "Server"
text= "front page"
Commandargument= "Prev"
Forecolor= "Navy"
Font-name= "Verdana" font-size= "8pt"

/>

<asp:linkbutton id= "Btnnext" runat= "Server"
text= "Back Page"
Commandargument= "Next"
Forecolor= "Navy"
Font-name= "Verdana" font-size= "8pt"

/>

<asp:linkbutton id= "Btnlast" runat= "Server"
Text= "Last"
Commandargument= "Last"
Forecolor= "Navy"
Font-name= "Verdana" font-size= "8pt"

/>


<p>
<asp:checkbox id= "Chk1" runat= "Server"
Text= "Show built in pages"
Font-name= "Verdana"
Font-size= "8pt"
Autopostback= "true"
/>

<p>
<table bgcolor= "#eeeeee" cellpadding= "6" ><tr><td nowrap><font face= "Verdana" size= "-2" >

<asp:label id= "Lblcurrentindex" runat= "Server"/><br>
<asp:label id= "Lblpagecount" runat= "Server"/><br>

</font></td></tr></table>
</form>

</body>

From the above example we can see that clicking the LinkButton control onclick triggers the Pagebuttonclick event, and the DataGrid page onpageindexchanged the change triggers the Mydatagrid_page event, We're going to write the code for these two events.

The following is the first query of the database information, using a function to express, because often used: I opened the table, hehe, is our studio admin Zone landing record (HA and sold a little of our studio secrets to Everyone)

ICollection CreateDataSource ()
{
/*
Read the database information and get the DataView
*/
SqlConnection myconnection = new SqlConnection ("SERVER=LOCALHOST;UID=SA;PWD=123456;DATABASE=ASPCN");
Sqldatasetcommand Mydatasetcommand = new Sqldatasetcommand ("SELECT * from Admin_enter ORDER BY enter_time Desc", Myconnect ION);
DataSet ds= new DataSet ();
Mydatasetcommand.filldataset (ds, "Admin_enter");
Return DS. tables["Admin_enter"]. DefaultView;
}

Then there is the Page_Load function, which is primarily to determine whether to display the paging numbers from the DataGrid, using the Pagestyle visible properties:

void Page_Load (Object sender, EventArgs e)
{
file:// whether to hide Pagerstyle-mode
if (chk1. Checked)
{
Mydatagrid.pagerstyle.visible=true;
}
Else
{
Mydatagrid.pagerstyle.visible=false;
}

Bindgrid ();
}

Here is the Pagerbuttonclick that handles the Click event, this is our core part, in fact we operate also just the CurrentPageIndex attribute of the DataGrid. If the CurrentPageIndex is less than pagecount then there is a next page, if CurrentPageIndex greater than 0 indicates a previous page.

void Pagerbuttonclick (Object sender, EventArgs e)
{
file://obtained LinkButton parameter value
String arg = ((LinkButton) sender). CommandArgument;

Switch (ARG)
{
Case ("Next"):
if (Mydatagrid.currentpageindex < (mydatagrid.pagecount-1))
Mydatagrid.currentpageindex + +;
Break
Case ("prev"):
if (Mydatagrid.currentpageindex > 0)
Mydatagrid.currentpageindex--;
Break
Case ("Last"):
Mydatagrid.currentpageindex = (mydatagrid.pagecount-1);
Break
Default
file://this page value
Mydatagrid.currentpageindex = arg. ToInt32 ();
Break
}
Bindgrid ();
}

Here is Mydatagrid_page, the main action is to call the Bindgrid function to give the data to the DataGrid display:

void Mydatagrid_page (Object sender, DataGridPageChangedEventArgs e)
{
file:// The method of pressing the numbers
Bindgrid ();
}

Finally there are two functions that they function, which I have commented on:

void Bindgrid ()
{
file://will DataView bound to the DataGrid.
Mydatagrid.datasource = CreateDataSource ();
Mydatagrid.databind ();
Showstats ();
}

void Showstats ()
{
file://Display page information
Lblcurrentindex.text = "Current page is:" + ((int) mydatagrid.currentpageindex+1);
Lblpagecount.text = "Total number of pages is:" + mydatagrid.pagecount;
}

So far, our personalized page has been completed (all code and display to see the section), our general idea is to use the LinkButton control as the flag of the page, by judging LinkButton commandargument value, Manipulate the CurrentPageIndex property of the DataGrid to achieve the effect of paging.

If you don't understand the structure in this section, see the entire code in the next section and the example.



Related Article

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.