DataList分页

容易么我, DataList分页在网上找了N个例子,分了三天,经历了N多挫折,心情从低谷到高山,又从高山到低谷,快弄出精神病了,好在有很多朋友帮忙,总算弄出来了,总结了下,发现N多的挫折竟然都是些很基础的知识,感叹啊,想提高快点真是要一步一个脚印,扎扎实实的.把代码贴出来,给以后要用留点参考.

后台:

 protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            panel1.Visible 
= true;

            
this.Session["tid"= "";

            GetDataBind(
"select * from land_showimages");

            CurrentPage 
= 0;
            ViewState[
"PageIndex"= 0;
            
//计算总共有多少记录
            RecordCount = CalculateRecord("");
            lblRecordCount.Text 
= RecordCount.ToString();
            
//计算总共有多少页

            
if (RecordCount % PageSize != 0)
            
{
                PageCount 
= RecordCount / PageSize + 1;
            }

            
else
            
{
                PageCount 
= RecordCount / PageSize;
            }


            lblPageCount.Text 
= (PageCount).ToString();
            ViewState[
"PageCount"= PageCount;
            myDropDownList.Items.Clear();
            DropDown();
        }

    }


    
int PageSize = 6, RecordCount, PageCount, CurrentPage;

    
private void GetDataBind(string sql)
    
{
        
int StartIndex;
        
//设定导入的起终地址
        StartIndex = CurrentPage * PageSize;

        DataSet ds 
= new DataSet();

        OracleConnection conn 
= new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();

        OracleCommand comm 
= new OracleCommand();
        comm.Connection 
= conn;
        comm.CommandText 
= sql;
        comm.CommandType 
= CommandType.Text;

        OracleDataAdapter myda 
= new OracleDataAdapter();
        myda.SelectCommand 
= comm;

        myda.Fill(ds, StartIndex, PageSize, 
"land_showimages");


        MyDataGrid.DataSource 
= ds.Tables["land_showimages"].DefaultView;
        MyDataGrid.DataBind();

        lbnFirstPage.Enabled 
= true;
        lbnNextPage.Enabled 
= true;
        lbnPrevPage.Enabled 
= true;
        lbnLastPage.Enabled 
= true;

        
if (CurrentPage == (PageCount - 1))
        
{
            lbnLastPage.Enabled 
= false;
            lbnNextPage.Enabled 
= false;
        }

        
if (CurrentPage == 0)
        
{
            lbnFirstPage.Enabled 
= false;
            lbnPrevPage.Enabled 
= false;
        }


        
if (PageCount == 1)
        
{
            lbnFirstPage.Enabled 
= false;
            lbnNextPage.Enabled 
= false;
            lbnPrevPage.Enabled 
= false;
            lbnLastPage.Enabled 
= false;
        }

        lblCurrentPage.Text 
= (CurrentPage + 1).ToString();
        ViewState[
"PageIndex"= CurrentPage;
    }


    
public int CalculateRecord(string tid)
    
{
        
int intCount;

        OraAccess ora 
= new OraAccess();
        DBParaList para 
= new DBParaList();

        
if (tid == "")
        
{
            intCount 
= ora.exeCountSql("land_showimages"new DBParaList());
        }

        
else
        
{
            para.AddPara(
"typeid", tid);
            intCount 
= ora.exeCountSql("land_showimages", para);
        }

        ora.close();
        
return intCount;
    }


    
public void Page_OnClick(Object sender, CommandEventArgs e)  //翻页按钮
    {
        CurrentPage 
= (int)ViewState["PageIndex"];
        PageCount 
= (int)ViewState["PageCount"];

        
string cmd = e.CommandName;
        
//判断cmd,以判定翻页方向
        switch (cmd)
        
{
            
case "next":
                
if (CurrentPage < (PageCount - 1)) CurrentPage++;
                
break;
            
case "prev":
                
if (CurrentPage > 0) CurrentPage--;
                
break;
            
case "last":
                CurrentPage 
= PageCount - 1;
                
break;
            
default:
                CurrentPage 
= System.Convert.ToInt32(cmd);
                
break;
        }

        ViewState[
"PageIndex"= CurrentPage;
        myDropDownList.SelectedIndex 
= CurrentPage;

        
if (this.Session["tid"== "")
        
{
            GetDataBind(
"select * from land_showimages");
        }

        
else
        
{
            GetDataBind(
"select * from land_showimages where typeid='" + this.Session["tid"+ "'");
        }

        panel1.Visible 
= true;

    }


    
private void DropDown()
    
{
        
for (int u = 0; u < PageCount; u++)
        
{
            myDropDownList.Items.Add(
new ListItem("" + (u + 1).ToString() + "", Convert.ToString(u)));
        }

    }


    
public void listchanged(object sender, System.EventArgs e) //页数下拉框
    {
        CurrentPage 
= myDropDownList.SelectedIndex;

        GetDataBind(
"select * from land_showimages where typeid='" + this.Session["tid"+ "'");
        panel1.Visible 
= true;

    }


    
public void ddltype_OnSelectedIndexchanged(object sender, System.EventArgs e)  //类别下拉框
    {
        
string sql;
       
        
if (ddltype.SelectedValue == "-1")
        
{
             sql 
= string.Format("select * from land_showimages");
             
this.Session["tid"= "";
        }

        
else
        
{
            sql 
= string.Format("select * from land_showimages where typeid={0}", ddltype.SelectedValue);
            
this.Session["tid"= ddltype.SelectedValue;

        }

  
        GetDataBind(sql);

        panel1.Visible 
= true;
        CurrentPage 
= 0;
        ViewState[
"PageIndex"= 0;
        
//计算总共有多少记录
        RecordCount = CalculateRecord(this.Session["tid"].ToString());
        lblRecordCount.Text 
= RecordCount.ToString();
        
//计算总共有多少页

        
if (RecordCount % PageSize != 0)
        
{
             PageCount 
= RecordCount / PageSize +1;
        }

        
else
        
{
            PageCount 
= RecordCount / PageSize;
        }


         lblPageCount.Text 
= (PageCount).ToString();
        ViewState[
"PageCount"= PageCount;
        myDropDownList.Items.Clear();
        DropDown();
    }

前台:

<div style="background-color: #ffffff">
            
<asp:DropDownList ID="ddltype" runat="server" AppendDataBoundItems="true" DataSourceID="SDSTYPE"
                AutoPostBack
="true" DataTextField="typename" DataValueField="id" Width="105px"
                OnSelectedIndexChanged
="ddltype_OnSelectedIndexchanged">
                
<asp:ListItem Text="--请选择类别--" Value="-1"></asp:ListItem>
            
</asp:DropDownList>
            
<asp:SqlDataSource ID="SDSTYPE" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                ProviderName
="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "ID", "TYPENAME" FROM "LAND_IMAGETYPE" ORDER BY "TYPENAME"'>
            
</asp:SqlDataSource>
            
<br />
            
<asp:DataList ID="MyDataGrid" runat="server" Width="100%" ItemStyle-VerticalAlign="Top"
                CaptionAlign
="Bottom" RepeatColumns="3" Height="320px" RepeatDirection="Horizontal">
                
<ItemTemplate>
                    
<table width="100%">
                        
<tr>
                            
<td align="center">
                                
<href="imagesDetail.aspx?id=<%#Eval("id") %>" target="_blank">
                                    
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("purl") %>' Width="100"
                                        Height="60" />
</a>
                                
<br />
                                
<asp:Label ID="Label1" runat="server" Text='<%# Eval("imagename") %>' Font-Size="12px"></asp:Label>
                            
</td>
                        
</tr>
                    
</table>
                
</ItemTemplate>
            
</asp:DataList>
            
&nbsp; &nbsp; &nbsp; &nbsp;<asp:Panel ID="panel1" runat="server" Visible="true">
                共有
<asp:Label ID="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp; 当前为<asp:Label
                    
ID="lblCurrentPage" ForeColor="red" runat="server" />/
                
<asp:Label ID="lblPageCount" ForeColor="red" runat="server" />&nbsp;
                
<asp:LinkButton ID="lbnFirstPage" Text="首 页" CommandName="0" OnCommand="Page_OnClick"
                    runat
="server" />
                
<asp:LinkButton ID="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick"
                    runat
="server" />
                
<asp:LinkButton ID="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick"
                    runat
="server" />
                
<asp:LinkButton ID="lbnLastPage" Text="末 页" CommandName="last" OnCommand="Page_OnClick"
                    runat
="server" />
                
<asp:DropDownList ID="myDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="listchanged">
                
</asp:DropDownList></asp:Panel>
        
</div>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值