Sometimes the gridview and datapager are used together with the bound database to finally read the database data, and the 'homepage-Previous Page-Next page-last page 'is displayed, however, when you click the buttons 'homepage-Previous Page-Next page-last page', you always need to click twice. In fact, the solution is very simple. You only need to bind the database once in the prerender event of the gridview.
Because the page first executes page_load and then gridview_prerender.
The following is a detailed example:
I. Front-end page
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> application of gridview </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "gridview2" runat = "server" onprerender = "gridview2_prerender">
<Columns>
<Asp: templatefield headertext = "ID">
<Edititemtemplate>
<Asp: textbox id = "textbox1" runat = "server" text = '<% # BIND ("ID") %>'> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # BIND ("ID") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "name">
<Edititemtemplate>
<Asp: textbox id = "textbox2" runat = "server" text = '<% # BIND ("name") %>'> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label2" runat = "server" text = '<% # BIND ("name") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
<Asp: datapager id = "datapager1" pagedcontrolid = "gridview2" pagesize = "15" runat = "server">
<Fields>
<Asp: nextpreviouspagerfield firstpagetext = "Homepage" showpreviouspagebutton = "true" showlastpagebutton = "false"
Shownextpagebutton = "false" showfirstpagebutton = "true"/>
<Asp: numericpagerfield/>
<Asp: templatepagerfield>
</ASP: templatepagerfield>
<Asp: nextpreviouspagerfield lastpagetext = "last page" showfirstpagebutton = "false" shownextpagebutton = "true"
Showpreviouspagebutton = "false" showlastpagebutton = "true"/>
</Fields>
</ASP: datapager>
</Div>
</Form>
</Body>
</Html>
2. Background page
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}
Public void BIND (){
Gridview2.datasource = userinfoservice. getuserinfo (); // This is the collection list of users you have written in the service layer.
Gridview2.databind ();
}
Protected void gridview2_prerender (Object sender, eventargs E)
{
BIND ();
}
}
However, the problem arises. If you want to perform another operation on this page, for example, if you want to query the operation, you will find that you click the buttons 'homepage-Previous Page-Next page-last page, click twice. To solve this problem, you can define a global variable bflag in the background to identify which operation you are performing and which event is triggered on the page. Example:
1. Add a textbox and button on the foreground page. The Code is as follows:
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txtname" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "select" onclick = "button#click"/> </div>
<Div>
<Asp: gridview id = "gridview2" runat = "server" onprerender = "gridview2_prerender">
<Columns>
<Asp: templatefield headertext = "ID">
<Edititemtemplate>
<Asp: textbox id = "textbox1" runat = "server" text = '<% # BIND ("ID") %>'> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # BIND ("ID") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "name">
<Edititemtemplate>
<Asp: textbox id = "textbox2" runat = "server" text = '<% # BIND ("name") %>'> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label2" runat = "server" text = '<% # BIND ("name") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
<Asp: datapager id = "datapager1" pagedcontrolid = "gridview2" pagesize = "15" runat = "server">
<Fields>
<Asp: nextpreviouspagerfield firstpagetext = "Homepage" showpreviouspagebutton = "true" showlastpagebutton = "false"
Shownextpagebutton = "false" showfirstpagebutton = "true"/>
<Asp: numericpagerfield/>
<Asp: templatepagerfield>
</ASP: templatepagerfield>
<Asp: nextpreviouspagerfield lastpagetext = "last page" showfirstpagebutton = "false" shownextpagebutton = "true"
Showpreviouspagebutton = "false" showlastpagebutton = "true"/>
</Fields>
</ASP: datapager>
</Div>
</Form>
</Body>
2. define a global variable bflag in the background, add a button to trigger the event button#click, and write another bindselect () method. The Code is as follows:
Public partial class _ default: system. Web. UI. Page
{
Int bflag = 0;
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}
Public void BIND (){
Gridview2.datasource = userinfoservice. getuserinfo ();
Gridview2.databind ();
}
Protected void gridview2_prerender (Object sender, eventargs E)
{
If (bflag = 1)
Bindselect ();
Else
BIND ();
}
Public void bindselect (){
Gridview2.datasource = userinfoservice. getuserinfobyselect (txtname. Text );
Gridview2.databind ();
}
Protected void button#click (Object sender, eventargs E)
{
Bflag = 1;
Bindselect (); // click the event to call
}
}