datalist

 今天學習了datalist列表:可以調用存儲過程實現分頁效果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data .SqlClient ;
using System.Configuration ;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
Bind("1");


}

private void Bind(string pageindex)
{
string str = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(str))
{
SqlDataAdapter da = new SqlDataAdapter("sp_book_Select_by_Page_rowNumber", sqlCnn);
da.SelectCommand.Parameters.AddWithValue("@pageIndex", pageindex);
da.SelectCommand.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.AddWithValue("@pageSize", 2);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
this.HiddenField1.Value = pageindex;
this.HiddenField2.Value = da.SelectCommand.Parameters["@pageCount"].Value.ToString();

}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Bind("1");
}
protected void Button2_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
if (index > 0)
index--;
this.Bind(index.ToString());
}
protected void Button3_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
int count = Convert.ToInt32(this.HiddenField2.Value);

if (index < count)
index++;
this.Bind(index.ToString());
}
protected void Button4_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(this.HiddenField2.Value);
this.Bind(count.ToString());
}
}
發佈了63 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章