欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gridview+objectdatasource+aspnetpager整合实例

程序员文章站 2024-03-04 09:11:59
前台代码: 复制代码 代码如下:

前台代码:

复制代码 代码如下:

<asp:gridview id="viewishow" runat="server" datasourceid="objectdatasource1"
                    allowpaging="true" autogeneratecolumns="false"
                    onrowcommand="viewishow_rowcommand" pagesize="2"
                    onpageindexchanging="viewishow_pageindexchanging"
                    ondatabound="viewishow_databound">

                    <columns>
                        <asp:boundfield datafield="title" headertext="名称" />
                        <asp:boundfield datafield="clicknum" headertext="播放次数" />
                        <asp:boundfield datafield="addtime" headertext="更新日期" />
                        <asp:templatefield headertext="操作">
                            <itemtemplate>
                                <asp:linkbutton id="linkbutton1" runat="server" commandname="up" commandargument='<%#eval("id") %>'>晋级</asp:linkbutton><asp:linkbutton
                                    id="linkbutton2" runat="server" commandname="down" commandargument='<%#eval("id") %>'>降级</asp:linkbutton>
                            </itemtemplate>

                        </asp:templatefield>
                    </columns>
                    <pagertemplate>
                        <table border="0" cellpadding="0" cellspacing="1" style="width: 100%; height: 100%;">
                            <tr style="height: 28px; padding-left: 20px;">
                                <td class="pages">
                                    <webdiyer:aspnetpager id="mypage" runat="server"
                                        onpagechanged="mypage_pagechanged" ></webdiyer:aspnetpager>
                                </td>
                            </tr>
                        </table>
                    </pagertemplate>
                </asp:gridview>
                <asp:objectdatasource id="objectdatasource1" runat="server"
                    dataobjecttypename="audiosystem.model.ishow" deletemethod="delete"
                    insertmethod="add" selectmethod="getlist" typename="audiosystem.bll.ishow"
                    updatemethod="update" onselected="objectdatasource1_selected">
                    <selectparameters>
                        <asp:parameter name="type" type="string" defaultvalue="week" />
                        <asp:controlparameter controlid="txtstarttime"
                            defaultvalue="2000/01/01 00:00:00" name="start"
                            propertyname="text" type="datetime" />
                        <asp:controlparameter controlid="txtendtime" name="end" propertyname="text"
                            type="datetime" defaultvalue="2500/01/01 00:00:00" />
                    </selectparameters>
                    <deleteparameters>
                        <asp:parameter name="videoid" type="int32" />
                        <asp:parameter name="type" type="string" defaultvalue="week" />
                    </deleteparameters>
                </asp:objectdatasource>


后台代码:

复制代码 代码如下:

protected void mypage_pagechanged(object sender, eventargs e)
         {
             gridviewrow pagerrow = viewishow.bottompagerrow;
             aspnetpager mypage = (aspnetpager)pagerrow.cells[0].findcontrol("mypage");
             viewishow.pageindex = mypage.currentpageindex - 1;
         }

         protected void viewishow_databound(object sender, eventargs e)
         {
             gridviewrow pagerrow = viewishow.bottompagerrow;
             aspnetpager mypage = (aspnetpager)pagerrow.cells[0].findcontrol("mypage");

             gridview view = (gridview)sender;
             mypage.pagesize = view.pagesize;
             mypage.currentpageindex = view.pageindex +1; //gridview分页索引是从0开始,aspnetpager是从1开始的
         }

         protected void objectdatasource1_selected(object sender, objectdatasourcestatuseventargs e)
         {
             dataset ds = (dataset)e.returnvalue; //objectdatasource查询方法返回什么类型就转成什么类型

             gridviewrow pagerrow = viewishow.bottompagerrow;
             aspnetpager mypage = (aspnetpager)pagerrow.cells[0].findcontrol("mypage");

             if (ds == null)
             {
                 mypage.recordcount = 0;
             }
             else
             {
                 mypage.recordcount = ds.tables[0].rows.count;
             }
         }