我想用datalist控件做分页效果,因为自己做不出来,所以就到网上找了一个现成的,经过修改后直接查询表里面的所有信息是可以用了,可是有一些还不是很理想,我是想把数据库里的一个表里面的某个字段的类型的内容查询出来,比如说属于新闻这个表里面的公司新闻。当中遇到了些问题,想请你帮改一改。
其实我想做到的功能是用同一个页面接收地址栏传来的表里面新闻所属类型的字段并显示该字段的所有信息。比如点公司新闻,或企业资讯等,显示属于该字段的所有信息。如果老师有时间,就麻烦你帮改一改,谢谢老师后台代码: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 _Default : System.Web.UI.Page
{
int PageSize, RecordCount, PageCount, CurrentPage;
OleDbConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
PageSize = 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;
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 News", conn);
int intCount = int.Parse(cmd.ExecuteScalar().ToString());
cmd.Dispose();
conn.Close();
return intCount;
}
private IList DataSource()
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
//string strSql = "select * from News"';//正常语句
string strSql = "select * from News where [News_Type] = 企业新闻 ";//(2)
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(ds, StartIndex, PageSize, "News");//用(2)时这里出现至少有一个参数不指定值
return ds.Tables["News"].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;
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();
}
}
}
其实我想做到的功能是用同一个页面接收地址栏传来的表里面新闻所属类型的字段并显示该字段的所有信息。比如点公司新闻,或企业资讯等,显示属于该字段的所有信息。如果老师有时间,就麻烦你帮改一改,谢谢老师后台代码: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 _Default : System.Web.UI.Page
{
int PageSize, RecordCount, PageCount, CurrentPage;
OleDbConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
PageSize = 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;
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 News", conn);
int intCount = int.Parse(cmd.ExecuteScalar().ToString());
cmd.Dispose();
conn.Close();
return intCount;
}
private IList DataSource()
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
//string strSql = "select * from News"';//正常语句
string strSql = "select * from News where [News_Type] = 企业新闻 ";//(2)
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(ds, StartIndex, PageSize, "News");//用(2)时这里出现至少有一个参数不指定值
return ds.Tables["News"].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;
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();
}
}
}
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 _Default : System.Web.UI.Page
{
int PageSize, RecordCount, PageCount, CurrentPage;
OleDbConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
PageSize = 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;
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 News", conn);
int intCount = int.Parse(cmd.ExecuteScalar().ToString());
cmd.Dispose();
conn.Close();
return intCount;
}
private IList DataSource(string tablename,string colname)//colname是字段名
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
//string strSql = "select * from News"';//正常语句
string strSql = "select * from" +tablename+" where [News_Type] = '" + colname + "' ";//(2)
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(ds, StartIndex, PageSize, "News");//用(2)时这里出现至少有一个参数不指定值
return ds.Tables["News"].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;
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();
}
}
}
编译器错误信息: CS1501: “DataSource”方法没有采用“0”个参数的重载源错误:行 64: private void mfbind()
行 65: {
行 66: DataList1.DataSource = (DataView)DataSource();
行 67: DataList1.DataBind();
行 68: np1.Enabled = true;
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/images/body_head/page_04.gif"
PostBackUrl="show_view.aspx?News_Type=公司荣誉" CausesValidation="False" />是这样吗?可还是出现上面的问题呢``
用String str = Request.QueryString("News_Type");
然后绑定的时候直接"...Where News_Type='"+ str +"';
这个值应该在前台进行绑定<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("要获取的字段名", "NewsPage.aspx?newstype={0}") %>' Text='<%# Eval("显示的字段名", "{0}") %>'></asp:HyperLink>
接收时出错"System.Web.HttpRequest.QueryString 是 属性 ,但此处被当做 方法 来使用
改为: ["News_Type"] 后以以查询到所要的类别了,但是却出现了一点问题,
1:因为查询的是表里面所有的信息,所以总的页数列出的为全部的,尽管显示页面是所但找出来的数据了,但实际中只有例出要查询的类别页数就可以了,如何修改源码,让它只显示某一类的页码呢?
2:表里面有三个类别的数据,企业新闻,最新资讯,公司荣誉,当点击"企业新闻"时,正常显示该类数据了,然后点击"最新资讯",当前的数据并没有跳转到"最新资讯"的数据,而依是"企业新闻"类别的数据,如果切换到主页再进行查询,数据能正常显示了,怎么改源码呢?
不好意思,出了点错误。。以前用的是VB,这个是C#
==
叫你建个类别表是指把你的分类放在这个类别表,所以你的类别只需要这一个表
通过你的类别到另一个页面只需要通过我给你的那个方法Request.QueryString直接获取类别就可以用一个页面来显示不同的类别了