如何获取和重写服务器控件的DataSource 用条件语句判断绑定哪个数据源,绑定后重新Bind()一下应该就可以了吧,没有试过,呵呵! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我要写一个分页控件,要获取分页控件绑定的服务器控件(如DataList)的数据源DataSource, 对数据源进行分页、统计,将进行过分页的数据源再绑定到服务器控件 DataList控件也玩分页 作者: 飞刀 人气:2568作者:http://www.aspcn.com 飞刀众所周知,ASP.Net中给我们提供了三个数据控件--DataGrid,Repeater,DataList。在这三个控件中,DataGrid控件的功能最强大,Repeater控件最忠实于模版原样,DataList控件则兼而有之。DataGrid控件太有名了,所以以前用的讲的也很多,Repeater功能太少,没有什么好讲的。这里主要是讲一讲DataList控件。DataList控件其实功能也很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像DataGrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!确实是一个很让人头疼的事情。不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。(本程序在.Net Framework Beta 2下测试通过)<% @ Page Language="C#" %><% @ Import Namespace="System.Data" %><% @ Import Namespace="System.Data.OleDb" %><Script Language="C#" Runat="Server">/* Create By 飞刀 http://www.aspcn.com 2001-7-25 01:44 Support .Net Framework Beta 2*/OleDbConnection MyConn;int PageSize,RecordCount,PageCount,CurrentPage;public void Page_Load(Object src,EventArgs e){ //设定PageSize PageSize = 10; //连接语句 string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;"; MyConn = new OleDbConnection(MyConnString); MyConn.Open(); //第一次请求执行 if(!Page.IsPostBack) { ListBind(); CurrentPage = 0; ViewState["PageIndex"] = 0; //计算总共有多少记录 RecordCount = CalculateRecord(); lblRecordCount.Text = RecordCount.ToString(); //计算总共有多少页 PageCount = RecordCount/PageSize; lblPageCount.Text = PageCount.ToString(); ViewState["PageCount"] = PageCount; }}//计算总共有多少条记录public int CalculateRecord(){ int intCount; string strCount = "select count(*) as co from Score"; OleDbCommand MyComm = new OleDbCommand(strCount,MyConn); OleDbDataReader dr = MyComm.ExecuteReader(); if(dr.Read()) { intCount = Int32.Parse(dr["co"].ToString()); } else { intCount = 0; } dr.Close(); return intCount;}ICollection CreateSource(){ int StartIndex; //设定导入的起终地址 StartIndex = CurrentPage*PageSize; string strSel = "select * from Score"; DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn); MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); return ds.Tables["Score"].DefaultView;}public void ListBind(){ score.DataSource = CreateSource(); score.DataBind(); lbnNextPage.Enabled = true; lbnPrevPage.Enabled = true; if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false; if(CurrentPage==0) lbnPrevPage.Enabled = false; lblCurrentPage.Text = (CurrentPage+1).ToString();}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; } ViewState["PageIndex"] = CurrentPage; ListBind();}</script><html><head><title></title></head><body><form runat="server">共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList id="score" runat="server"HeaderStyle-BackColor="#aaaadd"AlternatingItemStyle-BackColor="Gainsboro"EditItemStyle-BackColor="yellow"> <ItemTemplate> 姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %> <asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" /> </ItemTemplate></asp:DataList><asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" /><asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /></form></body></html> 你的意思应该是如何在自定义控件中实现数据绑定吧:http://aspnet.4guysfromrolla.com/articles/102903-1.aspx 例如:object _dataSource = Object.DataSource //获取数据绑定对象Object的DataSource的数据源...// 对_dataSource做分页处理...Object.DataSource = _dataSource // 将处理过的数据源重新绑定到Object对象 1、在你说的三步里你在哪里碰到问题了?2、是不是界面如DataList一次也没显示就要完成你的操作? 1、是不是界面如DataList一次也没显示就要完成你的操作?2、你可以说说你遇到的问题,是找不到对象还是对象不允许操作:) sql 批量导入excel数据 只能导入一条 我想把数据库表里的Name列都用函数转换生成Spell列,然后把这两列存进表里 c#执行关机 我要在页面的Page_Load里执行一个发邮件的函数,页面只有这个功能,防止别人重复刷新重复提交,请指教 C#执行CMD的命令有权限问题? 两个DataTable联合后,如何对数据排序? 如何得到当前属性的名字 这个网站收集的c#资源很全!! 关于 HyperLink ,谢谢参与 一个很难的问题,请高手帮忙,先谢谢了:) 服务器端调用问题? 请教: 如何固定DataGrid中列的宽度?
作者: 飞刀 人气:2568作者:http://www.aspcn.com 飞刀众所周知,ASP.Net中给我们提供了三个数据控件--DataGrid,Repeater,DataList。在这三个控件中,DataGrid控件的功能最强大,Repeater控件最忠实于模版原样,DataList控件则兼而有之。DataGrid控件太有名了,所以以前用的讲的也很多,Repeater功能太少,没有什么好讲的。这里主要是讲一讲DataList控件。DataList控件其实功能也很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像DataGrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!确实是一个很让人头疼的事情。不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。(本程序在.Net Framework Beta 2下测试通过)
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44 Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10; //连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open(); //第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0; //计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString(); //计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}ICollection CreateSource()
{ int StartIndex; //设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind(); lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();}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;
} ViewState["PageIndex"] = CurrentPage; ListBind();}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /></form>
</body>
</html>
http://aspnet.4guysfromrolla.com/articles/102903-1.aspx
object _dataSource = Object.DataSource //获取数据绑定对象Object的DataSource的数据源
...
// 对_dataSource做分页处理
...
Object.DataSource = _dataSource // 将处理过的数据源重新绑定到Object对象
2、是不是界面如DataList一次也没显示就要完成你的操作?
2、你可以说说你遇到的问题,是找不到对象还是对象不允许操作:)