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

亲密接触ASP.Net(14)

程序员文章站 2023-10-27 20:57:22
个性化分页(2) 上一节大家没有看到总的结构,这一节,我把代码全部贴出来,大家好好看看。 <%@ page language="c#" %> <%@ imp...
个性化分页(2)

上一节大家没有看到总的结构,这一节,我把代码全部贴出来,大家好好看看。

<%@ page language="c#" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sql" %>
<html>
<script language="c#" runat="server">

void page_load(object sender, eventargs e)
{
    //判断是否隐藏pagerstyle-mode
    if (chk1.checked)
    {
        mydatagrid.pagerstyle.visible=true;
    }
    else
    {
        mydatagrid.pagerstyle.visible=false;
    }

    bindgrid();
}

icollection createdatasource()
{
    /*
        读取的信息,获得dataview
    */
    sqlconnection myconnection = new sqlconnection("server=localhost;uid=sa;pwd=123456;database=cn");
    sqldatasetcommand mydatasetcommand = new sqldatasetcommand("select * from admin_enter order by enter_time desc",myconnection);
    dataset ds= new dataset();
    mydatasetcommand.filldataset(ds,"admin_enter");
    return ds.tables["admin_enter"].defaultview;
}

void pagerbuttonclick(object sender, eventargs e)
{
    //获得linkbutton的参数值
    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:
            //本页值
            mydatagrid.currentpageindex = arg.toint32();
            break;
    }
    bindgrid();
}

void mydatagrid_page(object sender, datagridpagechangedeventargs e)
{
    //处理按下数字的方法
    bindgrid();
}

void bindgrid()
{
    //将dataview绑定到datagrid上去
    mydatagrid.datasource = createdatasource();
    mydatagrid.databind();
    showstats();
}

void showstats()
{
    //显示页面信息
    lblcurrentindex.text = "当前页数为: " + ((int)mydatagrid.currentpageindex+1);
    lblpagecount.text = "总页数是: " + mydatagrid.pagecount;
}

</script>

<body>

<h3><font face="verdana">个性化的分页实例^&^</font></h3>

<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="工作室成员" datafield="enter_id" />
<asp:boundcolumn headertext="登陆时间" datafield="enter_time" />
</property>
</asp:datagrid>

<p>
<asp:linkbutton id="btnfirst" runat="server"
text="首页"
commandargument="0"
forecolor="navy"
font-name="verdana" font-size="8pt"
onclick="pagerbuttonclick"
/>
&nbsp;
<asp:linkbutton id="btnprev" runat="server"
text="前页"
commandargument="prev"
forecolor="navy"
font-name="verdana" font-size="8pt"
onclick="pagerbuttonclick"
/>
&nbsp;
<asp:linkbutton id="btnnext" runat="server"
text="后页"
commandargument="next"
forecolor="navy"
font-name="verdana" font-size="8pt"
onclick="pagerbuttonclick"
/>
&nbsp;

<asp:linkbutton id="btnlast" runat="server"
text="末页"
commandargument="last"
forecolor="navy"
font-name="verdana" font-size="8pt"
onclick="pagerbuttonclick"
/>


<p>
<asp:checkbox id="chk1" runat="server"
text="显示内置的页数"
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>

</html>

上面是全部的源代码,执行后的显示是:



图14-1

按下随便一个键,比如"末页"



图14-2

看,我们的程序就直接到了最后一页,即第5页,我们在下面的"当前页数"可以看到:)

我们再来点击"显示内置的页数"这个复选框,看看:



图14-3

看,我们前两节看到的那些小数字也出来了:)