ObjectList控件的自定义分页怎么写?

wdbxm 2007-01-29 07:19:05
请问这个控件的自定义分页怎么写啊?LoadItem()事件怎么写啊?望高手指点?(绑定DataReader)
可以给我一个简单的例子吗?谢谢~~
...全文
390 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jazzking746 2007-01-29
  • 打赏
  • 举报
回复
利用DataTable自身的Select方法,传入起始值,读取指定行数记录,最终实现翻页效果
/// <summary>
/// 读取指定ID段内的Mail
/// </summary>
/// <param name="startID">DataTable中开始记录的ID</param>
/// <returns></returns>
private void SelectTable(int startID)
{
try
{
DataTable dtAllData = this._dtAllData;
DataTable dtSelectData = new DataTable();
dtSelectData.Columns.Add("ProductID",typeof(String));
dtSelectData.Columns.Add("ProductName",typeof(String));
string strExpr;
int endID = startID + GlobalConfig.Size;
if(startID == 0)
{
strExpr = "ProductID >= "+ startID +" and ProductID <= "+ endID +"";
}
else
{
strExpr = "ProductID >= "+ startID +" and ProductID < "+ endID +"";
}
if(dtAllData != null && dtAllData.Rows.Count > 0)
{
DataRow[] foundRows = dtAllData.Select(strExpr);
string flag1 = foundRows.Length.ToString();
for(int i = 0; i < foundRows.Length; i ++)
{
DataRow row = dtSelectData.NewRow();
row["ProductID"] = foundRows[i][0];
row["ProductName"] = foundRows[i][1];
dtSelectData.Rows.Add(row);
}
if(dtSelectData != null && dtSelectData.Rows.Count > 0)
{
this.List.DataSource = dtSelectData;
this.List.DataBind();
}
try
{
DataTable dtPage = GetPage();
string selectPage = Page.Request["SelectPage"];
for(int i = 0; i < dtPage.Rows.Count; i++)
{
Link link = new System.Web.UI.MobileControls.Link();
if(i == Convert.ToInt32(selectPage))
{
link.Text = "[本页" + dtPage.Rows[i][0].ToString() + "]";
}
else
{
link.Text = "[" + dtPage.Rows[i][0].ToString() + "]";
}
link.NavigateUrl = dtPage.Rows[i][1].ToString();
link.ID = i.ToString();
link.BreakAfter = false;
this.Panel.Controls.Add(link);
}
}
catch(Exception exc)
{
this.lblMessage.Text = exc.Message;
}
}
else
{
this.lblMessage.Text = "没有记录!";
this.lblMessage.Visible = true;
}
}
catch(Exception exc)
{
this.lblMessage.Text = exc.Message;
}
}
#endregion

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet11 = new WapPager.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT ProductID, ProductName FROM Products";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Products(ProductName) VALUES (@ProductName); SELECT ProductID, Produc" +
"tName FROM Products WHERE (ProductID = @@IDENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductName", System.Data.SqlDbType.NVarChar, 40, "ProductName"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE Products SET ProductName = @ProductName WHERE (ProductID = @Original_Produ" +
"ctID) AND (ProductName = @Original_ProductName); SELECT ProductID, ProductName F" +
"ROM Products WHERE (ProductID = @ProductID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductName", System.Data.SqlDbType.NVarChar, 40, "ProductName"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductName", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductID", System.Data.SqlDbType.Int, 4, "ProductID"));
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Products WHERE (ProductID = @Original_ProductID) AND (ProductName = @" +
"Original_ProductName)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductName", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "server=127.0.0.1;database=Northwind;uid=sa;pwd=sa;timeout=300;";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Products", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ProductID", "ProductID"),
new System.Data.Common.DataColumnMapping("ProductName", "ProductName")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();

}
jazzking746 2007-01-29
  • 打赏
  • 举报
回复
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this._dtAllData = GetAllData();
SelectTable(Convert.ToInt32(Page.Request["startID"]));
}
}
#endregion

#region 获取全部数据,返回DataTable
/// <summary>
///GetAllData是获取全部数据,返回DataTable的方法,之所以采用DataTable来存放全部数
///据,主要是为了后面用DataTable的Select方法来获取其中指定范围内记录,你可以从多
///种途径获取这个DataTable,比如WebService等等;
/// </summary>
private DataTable GetAllData()
{
this.sqlDataAdapter1.Fill(this.dataSet11);
return this.dataSet11.Tables["Products"];
}
#endregion

#region 通过数据总数、每页规定显示数量,计算出页数的方法
/// <summary>
/// GetPage是通过数据总数、每页规定显示数量,计算出页数的方法,返回存放全部页码的
/// DataTable,我们为这个DataTable定义了LinkText和LinkUrl两列,这两列的值供页码
/// 控件Link绑定数据所用。
/// </summary>
private DataTable GetPage()
{
DataTable dtPage = new DataTable();
DataTable dtAllData = this._dtAllData;
int dataCount = dtAllData.Rows.Count;
int pageCount = 0;
if((dataCount%GlobalConfig.Size) == 0)
{
pageCount = dataCount/GlobalConfig.Size;
}
if((dataCount%GlobalConfig.Size) != 0)
{
pageCount = dataCount/GlobalConfig.Size + 1;
}
dtPage.Columns.Add("LinkText",typeof(String));
dtPage.Columns.Add("LinkUrl",typeof(String));
for(int i = 0;i < pageCount; i++)
{
DataRow row = dtPage.NewRow();
row["LinkText"] = i + 1;
if(i == 0)
{
row["LinkUrl"] = "MobileWebForm1.aspx?startID=" + ((i*GlobalConfig.Size)) + "&selectPage=" + i;
}
else
{
row["LinkUrl"] = "MobileWebForm1.aspx?startID=" + ((i*GlobalConfig.Size) + 1) + "&selectPage=" + i;
}
dtPage.Rows.Add(row);
}
return dtPage;
}

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧