现在的位置: 首页 > 综合 > 正文

分析DataList分页和超级链接技术

2013年10月20日 ⁄ 综合 ⁄ 共 6850字 ⁄ 字号 评论关闭
由于要做项目,所以好久了都没上自己的博客了,不知道我的博客有没有经常来的朋友啊?如果有谢谢你们,有空常来啊,可以给我留言啊,有什么不对的地方可以提醒我改正哦。今天本来想把那个TreeView加框架研究出来的,但是师姐的代码,让我有点吃不消,所以还是没钻研出来怎么把那种效果做出来,还需研究,不过,今天实现了一个很重要的功能,相信大家还是平时做的时候会经常用到的,虽然ASP.NETDataGrid的功能很强大,但是Repeater,DataList很灵活啊,可以定义自己的样式出来,DataList可以说是Repeater的强化版吧,功能更强大。今天研究了下用DataList做分页技术,还有使用其中的一列做超级链接,因为大家知道,DataList是没提供分页属性的,同时它也没有DataGrid中的超级链接列,所以需要自己写代码的,两个技术点先给大家列出来,希望对大家有帮助,当然我在做这个的时候也参考了别人的做法的,大家互相学习嘛。

     技术点一:分页主要用到了sda.Fill(System.Data.DataSet ds ,int startRecord,int maxRecords,string srcTable)第一个重载方法。

 

      技术点二:让绑定列一列成为一个超级链接列。

 

HTML端代码:<%#show(DataBinder.Eval(Container.DataItem,OrderID))%>

 

在后台代码是一个show()的方法:

 

public string show (object orderID)

 

{

 

return "<a href=WebForm2.aspx?id="+orderID+" target='_blank'>"+orderID+" </a>";

 

}

 

     主要是这两个技术点了,现在给大家看详细的代码,我运行通过的,以后用的时候就可以COPY了,偷下懒,嘿嘿~~~

 

      第一个页面分页技术的:

 

前台代码如下:

 

<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 752px; POSITION: absolute; TOP: 16px; HEIGHT: 312px" cellSpacing="0" cellPadding="0" width="752" border="0">

 

<TR>

 

              <TD style="HEIGHT: 29px"><FONT face="宋体">DataList分页技术和超级链接</FONT></TD>

 

</TR>

 

<TR>

 

           <TD style="HEIGHT: 252px"><asp:datalist id="DataList1" runat="server" Width="576px"

 

 

Height="96px">

 

                                                 <HeaderTemplate>

 

                                                        定单编号<td>

 

                                                        员工编号<td>

 

                                                        定单日期<td>

 

                                                        运费<td>

 

                                                        运往所在城市

 

                                                 </HeaderTemplate>

 

                                                 <ItemTemplate>

 

<%# show(DataBinder.Eval(Container.DataItem,"OrderID"))%>

 

                                                        <td>

<%# DataBinder.Eval(Container.DataItem,"CustomerID")%>

 

                                                        <td>

<%# DataBinder.Eval(Container.DataItem,"OrderDate")%>

 

                                                        <td>

 

<%# DataBinder.Eval(Container.DataItem,"Freight")%>

 

                                                        <td>

<%# DataBinder.Eval(Container.DataItem,"ShipCity")%>

 

                                                 </ItemTemplate>

 

                                          </asp:datalist></TD>

 

                            </TR>

 

                            <TR>

 

                                   <TD><FONT face="宋体">

<asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton>&nbsp;

 

<asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="NextLB" runat="server" OnCommand=LinkButton_Click

 

CommandName="next">下一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="EndLB" runat="server" OnCommand=LinkButton_Click

 

CommandName="end">最后一页</asp:linkbutton>&nbsp;&nbsp;

<asp:label id="TotalLbl" runat="server"></asp:label> 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>

 

<asp:linkbutton id="JumpLB" runat="server" OnCommand=LinkButton_Click CommandName="jump">跳到</asp:linkbutton>

 

       <asp:textbox id="TextBox1" runat="server" Width="90px"></asp:textbox>

 

</FONT></TD>

 

                            </TR>

 

</TABLE>

后台代码如下:

private void Page_Load(object sender, System.EventArgs e)

 

        {

 

            // 在此处放置用户代码以初始化页面

 

            PageSize = 12;//每页12条记录

 

 

            if(!Page.IsPostBack)

 

            {

 

                this.DataListBind();

 

                CurrentPage = 0;//当前页习惯设为0

 

                ViewState["PageIndex"] = 0;//页索引也设为0

 

 

                //计算总共有多少记录

 

                RecordCount = CalculateRecord();

 

 

                //计算总共有多少页

 

                PageCount = RecordCount/PageSize;

 

                this.TotalLbl.Text = PageCount.ToString();//显示总页数

 

                ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session

 

 

            }

 

        }

 

       

 

   

 

   

 

       

 

        //计算总共有多少条记录

 

        private  int CalculateRecord()

 

        {

 

            try

 

            {

 

                int recordCount;

 

                SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");//数据库使用Northwind;

 

                con.Open();

 

                string sql= "select count(*) as count from Orders";

 

                SqlCommand cmd = new SqlCommand(sql,con);

 

                SqlDataReader sdr = cmd.ExecuteReader();

 

                if(sdr.Read())

 

                {

 

                    recordCount = Int32.Parse(sdr["count"].ToString());

 

                }

 

                else

 

                {

 

                    recordCount = 0;

 

                }

 

                sdr.Close();

 

                con.Close();

 

                return recordCount;

 

            }

 

            catch(Exception ex)

 

            {

 

                throw new Exception(ex.Message);

 

            }

 

        }

 

        //以第一列定单号做个超级链接,链接到第二页显示定单详细信息

 

        public string show (object orderID)

 

        {

 

         return "<a href=WebForm2.aspx?id="+orderID+" target='_blank'>"+orderID+" </a>";

 

        }

 

        //将数据绑定到Datalist控件

 

        public void DataListBind()

 

        {

 

            try

 

            {

 

               

 

                int StartIndex = CurrentPage*PageSize;//设定导入的起终地址

 

                string sql = "select * from Orders";

 

                DataSet ds = new DataSet();

 

                SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");

 

                con.Open();

 

                SqlDataAdapter sda= new SqlDataAdapter(sql,con);

 

               

 

                sda.Fill(ds,StartIndex,PageSize,"orders");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName

 

                this.DataList1.DataSource = ds.Tables["orders"].DefaultView;

 

                this.DataList1.DataBind();

 

                this.PreviousLB.Enabled = true;

 

                this.NextLB.Enabled = true;

 

                if(CurrentPage==(PageCount-1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用

 

                if(CurrentPage==0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用

 

                this.CurrentLbl.Text = (CurrentPage+1).ToString();//当前页数

 

            }

 

            catch(Exception ex)

 

            {

 

                throw new Exception(ex.Message);

 

            }

 

 

        }

 

 

        public void LinkButton_Click(Object sender,CommandEventArgs e)//自己编写的按钮点击事件

 

        {

 

            CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引

 

            PageCount = (int)ViewState["PageCount"];//获得总页数

 

 

            string cmd = e.CommandName;

 

            //判断cmd,以判定翻页方向

 

            switch(cmd)

 

            {

 

                case "prev"://上一页

 

                    if(CurrentPage>0) CurrentPage--;

 

                    break;

 

                case "next":

 

                    if(CurrentPage<(PageCount-1)) CurrentPage++;//下一页

 

                    break;

 

                case "first"://第一页

 

                    CurrentPage=0;

 

                    break;

 

                case "end"://最后一页

 

                    CurrentPage=PageCount-1;

 

                    break;

 

抱歉!评论已关闭.