datalist分页效果问题

        我想用datalist控件做分页效果,因为自己做不出来,所以就到网上找了一个现成的,经过修改后直接查询表里面的所有信息是可以用了,可是有一些还不是很理想,我是想把数据库里的一个表里面的某个字段的类型的内容查询出来,当中遇到了些问题,最终要实现的功能是用同一个页面接收地址栏传来的表里面新闻所属类型的字段并显示该字段的所有信息,比如点公司新闻,或企业资讯等,显示属于该字段的所有信息。经过在网上找方法最终得到了想要的效果,但还是有些不理想.

        1:因为查询的是表里面所有的信息,所以总的页数列出的为这个表里全部的页码,尽管显示页面信息是所要查询出来的数据了,但实际应用中只要例出要查询的类别页数和页码就可以了,如何修改源码,只让它只显示某一类的页码呢?

         2:表里面有三个类别的数据,企业新闻,最新资讯,公司荣誉,当点击"企业新闻"时,正常显示该类数据了,然后点击"最新资讯",当前的数据并没有跳转到"最新资讯"的数据,而依然是"企业新闻"类别的数据,如果切换到主页或别的类别再进行查询,数据能正常显示了,怎么改源码呢?

        其实可以单独为每一个类建立一个类别表,或者使用SQL语句从表中使用Distinct只显示一次同一种类别名,如果这样显示应该在主页面不会显示你要查看的内容而只是类别,然后通过连接从类别跳到内容显示页面。但实际上这样的方法不理想,在实际应用中`一个数据库要是有N个类别的数据,要是真的每个类分建一张表那到可想而知不易``还有呢``还有要是这样做,不是要为查询每一个类的数据要建每一个类别的查询网页吧```这看起来重复做相同网页了````

如何才能做到我想要的效果呢?

图片按钮传值页面代码:

<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/images/body_head/page_04.gif"                 PostBackUrl="show_view.aspx?News_Type=1" CausesValidation="False" />

前台数据绑定代码:

<asp:DataList ID="DataList1" runat="server" >                                 <ItemTemplate>                                     <div style="width: 650px; height: 100%; background-color: white; text-align: center; vertical-align: middle;">                                         <br />                                         <div style="width: 630px; height: 12px; text-align: left" id="DIV1" οnclick="return DIV1_onclick()">                                             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/new.gif" />                                             <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ID", "view.aspx?ID={0}") %>'                                                 Text='<%# Eval("Nesw_tille") %>' Font-Size="14pt"></asp:HyperLink><span style="color: #ffcc66"></span></div>                                             <table border="1" style="width: 560px; border-top-style: none; border-right-style: none; border-left-style: none; height: 100%; border-bottom-style: none">                                             <tr>                                               <td align="left" valign="top"  style="BACKGROUND: #F5F5F5; COLOR: #333333; width: 460px; height: 100%; border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none;" οnclick="Redirect('News_4612.Html');" οnmοuseοver="this.style.background='#FFF8E8';this.style.color='#333333';" οnmοuseοut="this.style.background='#F5F5F5';this.style.color='#666666';">                                                   <asp:Label ID="Label1" runat="server" Text='<%# Eval("News_zhaiyao") %>' Width="600px"></asp:Label><div style="width: 600px; height: 13px; table-layout: fixed; cursor: hand;">                                                   类别:<asp:Label ID="News_TypeLabel" runat="server" Text='<%# Eval("News_Type") %>'></asp:Label>&nbsp; 时间:<asp:Label ID="DateTimeLabel" runat="server" Text='<%# Eval("DateTime") %>'></asp:Label>                                             &nbsp; | 查看:<asp:Label ID="BrowsTimeLabel" runat="server" Text='<%# Eval("BrowsTime") %>'></asp:Label>                                             &nbsp;|编辑:<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>'></asp:Label><span                                                 style="color: #ffcc66"></span></div>                                               </td>   </tr>                                         </table>                                         <br />                                     </div>                                 </ItemTemplate>                             </asp:DataList><br />                             <asp:Panel ID="Panel1" runat="server" Height="0px" Width="700px">                                 <asp:LinkButton ID="fp1" runat="server" CommandName="first">首页</asp:LinkButton>                                 &nbsp;<asp:LinkButton                                     ID="pp1" runat="server" CommandName="prev">上一页</asp:LinkButton>&nbsp;                                 &nbsp;<asp:LinkButton ID="np1" runat="server" CommandName="next">下一页</asp:LinkButton>&nbsp;                                 &nbsp;<asp:LinkButton ID="lp1" runat="server" CommandName="last">尾页</asp:LinkButton>&nbsp;                                 &nbsp;<asp:Label ID="Label1" runat="server">页次</asp:Label>&nbsp; &nbsp;<asp:Label ID="cp1" runat="server"></asp:Label>&nbsp; &nbsp;<asp:Label ID="Label2" runat="server" Width="3px">/</asp:Label>                                 <asp:Label ID="pc1" runat="server"></asp:Label>&nbsp; &nbsp;<asp:Label ID="Label3" runat="server">每页记录数</asp:Label>&nbsp; &nbsp;<asp:Label ID="rc1" runat="server"></asp:Label>&nbsp;                                 &nbsp;<asp:Label ID="Label4" runat="server">选择跳转页码</asp:Label>&nbsp; &nbsp;<asp:DropDownList ID="jp1" runat="server" AutoPostBack="True">                                 </asp:DropDownList>                                 </asp:Panel>

 

后台文件代码:

using System; using System.Data; using System.Configuration; using System.Collections; 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.OleDb;

namespace MikeCat_app2 {

    public partial class show_type : System.Web.UI.Page     {         int PageSize;//每页条数         int RecordCount;//总条数         int PageCount;//总页数         int CurrentPage;//当前页数         OleDbConnection conn;         private void Page_Load(object sender, System.EventArgs e)         {             // 在此处放置用户代码以初始化页面             PageSize = 6;//每页10条记录             string connstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(".") + "//App_Data//stu_data.mdb;";             conn = new OleDbConnection(connstr);             rc1.Text = PageSize.ToString();             CurrentPage = 0;//当前页习惯设为0             RecordCount = this.GetRecordCount();             PageCount = (RecordCount % PageSize) > 0 ? (RecordCount / PageSize) + 1 : (RecordCount / PageSize);             pc1.Text = PageCount.ToString();//计算总页数             ArrayList al = new ArrayList();             for (int i = 1; i < PageCount + 1; i++)             {                 al.Add(i);             }

            if (!Page.IsPostBack)             {                 jp1.DataSource = al;                 jp1.DataBind();                 mfbind();             }         }         public int GetRecordCount()         {             conn.Open();             OleDbCommand cmd = new OleDbCommand("select count(*) as cnt from chanpin", conn);             int intCount = int.Parse(cmd.ExecuteScalar().ToString());             cmd.Dispose();             conn.Close();             return intCount;         }         //将数据绑定到Datalist控件         private IList DataSource()         {             String str = Request.QueryString["News_Type"];             int StartIndex = CurrentPage * PageSize;//设定导入的起终地址             string strSql = "select * from chanpin where [News_Type] = '" + str + " ' ";             DataSet ds = new DataSet();             OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);             da.Fill(ds, StartIndex, PageSize, "chanpin");//da.Fill方法第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName             return ds.Tables["chanpin"].DefaultView;         }         private void mfbind()         {             DataList1.DataSource = (DataView)DataSource();             DataList1.DataBind();             np1.Enabled = true;             pp1.Enabled = true;             if (CurrentPage == (PageCount - 1)) np1.Enabled = false;//当为最后一页时,下一页链接按钮不可用             if (CurrentPage == 0) pp1.Enabled = false;//当为第一页时,上一页按钮不可用             cp1.Text = (CurrentPage + 1).ToString();//当前页数         }         public void Page_OnClick(Object sender, CommandEventArgs e)//自己编写的按钮点击事件         {             CurrentPage = int.Parse(cp1.Text.Trim()) - 1;//获得当前页索引             PageCount = int.Parse(pc1.Text.Trim());//获得总页数

            string cnstr = e.CommandName;//判断cnstr,以判定翻页方向             switch (cnstr)             {                 case "first":                     CurrentPage = 0;//第一页                     break;                 case "next":                     if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页                     break;                 case "prev":                     if (CurrentPage > 0) CurrentPage--;//上一页                     break;                 case "last":                     CurrentPage = PageCount - 1;//最后一页                     break;             }             mfbind();

        }         #region Web 窗体设计器生成的代码         override protected void OnInit(EventArgs e)         {             // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。             InitializeComponent();             base.OnInit(e);         }

        /// <summary>         /// 设计器支持所需的方法 - 不要使用代码编辑器修改         /// 此方法的内容。         /// </summary>         private void InitializeComponent()         {             this.fp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);             this.pp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);             this.np1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);             this.lp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);             this.jp1.SelectedIndexChanged += new System.EventHandler(this.jp1_SelectedIndexChanged);             this.Load += new System.EventHandler(this.Page_Load);         }         #endregion         private void jp1_SelectedIndexChanged(object sender, System.EventArgs e)         {             CurrentPage = int.Parse(jp1.SelectedItem.Text.Trim()) - 1;             mfbind();         }

    } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值