DataList排版技巧及自定义分页样式。

DataList排版技巧及自定义分页样式。

效果:

 

aspx.cs代码:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
            
if (!IsPostBack)
            
{
                myDataList.DataSource 
= pds();
                myDataList.DataBind();
            }

    }


    
private PagedDataSource pds()
    
{
       
//try
       
// {
            SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlDataAdapter da 
= new SqlDataAdapter("select * from [book]", myconn);
            DataSet ds 
= new DataSet();
            da.Fill(ds, 
"book");

            PagedDataSource pds 
= new PagedDataSource();
            pds.DataSource 
= ds.Tables["book"].DefaultView;
            pds.AllowPaging 
= true;
            pds.PageSize 
= 4;
            pds.CurrentPageIndex 
= Convert.ToInt32(Request.QueryString["page"]);
        
//}
        
//catch (Exception ex)
        
//{
        
//    Response.Write(ex);
        
//}
        return pds;
    }


    
protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    
{
        
if (e.Item.ItemType == ListItemType.Footer)
        
{//判断当前项是页脚模板
            int n = pds().PageCount;//将分页总数赋给变量n
            int i = pds().CurrentPageIndex;//将当前分页码赋给i
            Label lblpc = (Label)e.Item.FindControl("lblpc");
            lblpc.Text 
= n.ToString();
            
//找到lblpc这个Label,将总页码赋给他
            Label lblp = (Label)e.Item.FindControl("lblp");
            lblp.Text 
= Convert.ToString(i + 1);
            
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
            Label lblper = (Label)e.Item.FindControl("lblper");
            lblper.Text 
= Convert.ToString(pds().PageSize);
            HyperLink hlfir 
= (HyperLink)e.Item.FindControl("hlfir");
            hlfir.NavigateUrl 
= "?page=0";
            HyperLink hlla 
= (HyperLink)e.Item.FindControl("hlla");
            hlla.NavigateUrl 
= "?page=" + Convert.ToInt32(n - 1);
            
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
            HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
            HyperLink hln 
= (HyperLink)e.Item.FindControl("hln");
            
//找到表示上页和下页这两个控件

            
if (i <= 0)
            
{//如果当前页已经是第0页
                hlfir.Enabled = false;
                hlp.Enabled 
= false;
                hln.Enabled 
= true;
            }

            
else
            
{
                hlp.NavigateUrl 
= "?page=" + Convert.ToInt32(i - 1);
            }


            
if (i > n - 2)
            
{//如果当前项已经是最末页
                hln.Enabled = false;
                hlla.Enabled 
= false;
                hlp.Enabled 
= true;
            }

            
else
            
{
                hln.NavigateUrl 
= "?page=" + Convert.ToInt32(i + 1);
            }


            DropDownList ddlpage 
= (DropDownList)e.Item.FindControl("ddlpage");
            
for (int p = 0; p < n; p++)
            
{
                ddlpage.Items.Add(Convert.ToString(p 
+ 1));
                
if (p == i)
                
{
                    ddlpage.SelectedIndex 
= p;
                }

            }

        }

    }


    
protected void ddl_SelectedChanged(object sender, EventArgs e)
    
{
        DropDownList ddl 
= (DropDownList)sender;
        Page.Response.Redirect(
"?page=" +(Convert.ToInt32(ddl.SelectedItem.Value)-1));
    }


}


aspx代码:
<% @ 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 > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >

        
< asp:DataList ID = " myDataList "  runat = " server "  RepeatColumns = " 2 "  OnItemDataBound = " myDataList_ItemDataBound " >
        
< ItemTemplate >
        
        
< table cellpadding = " 10 "  style = " font: 9pt 宋体 " >
          
< tr >
            
< td style = " width:1px; background-color:#eeeeef "   />

            
< td valign = " top "  align = " center " >
              
< img src = ' <%# DataBinder.Eval(Container.DataItem, "images") %> '  alt = " 图书信息 "   />< p >
                  
< asp:LinkButton ID = " LinkButton1 "  runat = " server " > 购买此书 </ asp:LinkButton ></ p >
                
            
</ td >

            
< td valign = " top " >

              
< b > 书名: </ b ><% # DataBinder.Eval(Container.DataItem,  " bookname " %>< br >
              
< b > 类别: </ b ><% # DataBinder.Eval(Container.DataItem,  " style " %>< br >
              
< b > 出版商 ID: </ b ><% # DataBinder.Eval(Container.DataItem,  " publisher " %>< br >
              
< b > 出版时间: </ b ><% # DataBinder.Eval(Container.DataItem,  " publishtime " %>< br >
              
< b > 作者: </ b ><% # DataBinder.Eval(Container.DataItem,  " author " %>

              
< p >

              说明:
<% # DataBinder.Eval(Container.DataItem,  " introduction " %>
              
</ a >

            
</ td >
          
</ tr >
          
< tr >< td colspan = " 3 "  style = " height:1px; background-color:red " ></ td ></ tr >
        
</ table >


        
</ ItemTemplate >
                
< FooterTemplate >
        
< table width = " 100% " >< tr >
        
< td style = " font-size: 9pt; color: #000000; background-color: #fcd699; "  align = " center " > 共  < asp:Label ID = " lblpc "  runat = " server "  Text = " Label " ></ asp:Label >  页  当前第  < asp:Label ID = " lblp "  runat = " server "  Text = " Label " ></ asp:Label >  页  
                每页显示 
< asp:Label ID = " lblper "  runat = " server "  Text = " Label " ></ asp:Label >  项   & nbsp;  & nbsp;  & nbsp;  & nbsp; 
              
< asp:HyperLink ID = " hlfir "  runat = " server "  Text = " 首页 " ></ asp:HyperLink >   & nbsp; 
              
< asp:HyperLink ID = " hlp "  runat = " server "  Text = " 上一页 " ></ asp:HyperLink >& nbsp;  & nbsp; 
              
< asp:HyperLink ID = " hln "  runat = " server "  Text = " 下一页 " ></ asp:HyperLink >& nbsp;  & nbsp; 
              
< asp:HyperLink ID = " hlla "  runat = " server "  Text = " 末页 " ></ asp:HyperLink >& nbsp;  & nbsp;  & nbsp;  & nbsp; 
              跳转到
< asp:DropDownList ID = " ddlpage "  runat = " server "  AutoPostBack = " true "  OnSelectedIndexChanged = " ddl_SelectedChanged " >
            
</ asp:DropDownList >
            
</ td >
        
</ tr >  
        
</ table ></ FooterTemplate >
        
</ asp:DataList >
            
    
</ div >
    
</ form >
</ body >
</ html >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值