Some time ago, the function of making a message board requires you to check the page flip of the message list without refreshing the new page. Therefore, the following function is provided, although simple, involves a lot of knowledge, I will record the production steps and experiences for your reference only. If there are any deficiencies, I hope you can point out that there are better ways to learn from them.
1. Create a paging stored procedure:
Create procedure [DBO]. [p_pager] (@ pagenumber int, @ pagesize INT) as declare @ SQL nvarchar (4000) set @ SQL = 'select top' + convert (varchar, @ pagesize) + '* From t_test where [type] = 1 and id not in (select top' + convert (varchar, (@ pagenumber-1) * @ pagesize) + 'id from t_test order by id desc) order by id desc 'exec sp_executesql @ sqlgo
2. Set the Ajax Control
Ajax will certainly be used for implementation without refreshing the content. The first step is to add the necessary:
<Asp: scriptmanager id = "scriptmanager1" runat = "server"> </ASP: scriptmanager>
Then embed the Repeater control into updatepanel:
<Asp: updatepanel id = "updatepanel1" runat = "server"> <contenttemplate> <ul> <asp: repeater id = "repeater1" runat = "server"> <itemtemplate> <li> <p> <span> <% # eval ("username") %>: </span> <% # eval ("content "). tostring () %> </P> <p> <em> posting time: <% # eval ("addtime ") %> </em> </P> </LI> </itemtemplate> </ASP: repeater> </ul> <p> total record: <asp: literal id = "ltlcount" runat = "server"> </ASP: literal> </P> <p> <webdiyer: aspnetpager id = "aspnetpager1" runat = "server" alwaysshow = "true" pageindexboxtype = "dropdownlist"> </webdiyer: aspnetpager> </P> </div> </contenttemplate> </ASP: updatepanel>
3. aspnetpager Page Control
You can see the aboveCodeThere is a paging control, and I believe it is no stranger to everyone. I will not introduce it much. Aspnetpager. dll must be available and referenced in the project. Introduce this code in the header:
<% @ Register Assembly = "aspnetpager" namespace = "Wuqi. webdiyer" tagprefix = "webdiyer" %>
At this time, the paging control should be ready for use. Finally, bind the data in the background and the aspnetpager control. The complete background code is as follows:
Using system; using system. collections. generic; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. data; using Wuqi. webdiyer; using system. data. sqlclient; public partial class ajaxpager: system. web. UI. page {int currentpagenumber; // the current page number int pagesize = 5; // number of records displayed on each page protected void page_load (Object sender, eventargs e) {If (! Ispostback) {currentpagenumber = 1; viewstate ["currentpagenumber"] = currentpagenumber; binddata ();} aspnetpager1.pagechanged + = new eventhandler (aspnetpager1_pagechanged ); // define control paging events} // obtain the total number of records private int getcount () {string SQL = "select count (*) from t_test"; datatable dt = gettable (SQL, commandtype. text, values); If (DT. rows. count> 0) {return convert. toint32 (DT. rows [0] [0]);} else {return 0 ;}// bind the private void binddata () {ltlcount. TEXT = getcount (). tostring (); currentpagenumber = convert. toint32 (viewstate ["currentpagenumber"]); sqlparameter [] values = {New sqlparameter ("@ pagenumber", currentpagenumber), new sqlparameter ("@ pagesize", pagesize )}; datatable dt = gettable ("p_pager", commandtype. storedprocedure, values); // call the stored procedure if (DT. rows. count> 0) {aspnetpager1.pagesize = Pagesize; aspnetpager1.recordcount = getcount (); aspnetpager1.currentpageindex = currentpagenumber; this. repeater1.datasource = DT. defaultview; this. callback () ;}// pagination event void aspnetpager;pagechanged (Object sender, eventargs e) {currentpagenumber = aspnetpager1.currentpageindex; viewstate ["currentpagenumber"] = currentpagenumber; binddata ();} // read the stored procedure and return the table private datatable getta Ble (string SQL, commandtype T, Params sqlparameter [] values) {using (sqlconnection conn = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; persist Security info = true; user ID = sa; Password = 123456 ") {sqlcommand comm = new sqlcommand (SQL, Conn); Comm. commandtype = T; If (values! = NULL & values. length> 0) Comm. parameters. addrange (values); sqldataadapter da = new sqldataadapter (Comm); dataset DS = new dataset (); try {Conn. open (); DA. fill (DS); Return Ds. tables [0];} catch (exception) {return NULL;} finally {Conn. close (); Conn. dispose ();}}}}
at this point, the basic success has been achieved. Although it is relatively simple, it is worth collecting and learning about stored procedures, Ajax, paging controls, and other knowledge points, I hope that my Article will be the motivation for learning. If you are hungry for knowledge, then you are foolish with an empty mind.