
表名:ChinaStates
控件:Repeater
查詢代碼DA:
public class ChinaStatesDA
{
PRivate DataClassesDataContext Context; // 構建LINQ
public ChinaStatesDA()
{
Context = new DataClassesDataContext();
}
public List<ChinaStates> Select(int nowye,int numbers)
// 輸入當前頁,頁面數據條數,查詢數據庫信息
{
return Context.ChinaStates.Skip((nowye-1)*numbers).Take(numbers).ToList();
// .Skip 是跳過多少條數據查詢 .Take查詢前幾條數據
}
public int Select() // 查詢數據庫有多少條數據
{
return Context.ChinaStates.Count();
}
}
Cs 代碼、;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) // 運行界面
{
if (!IsPostBack)
{
bindchinadata(1,5); // 綁定數據①
int count = new ChinaStatesDA().Select(); //獲取數據庫多少條數據
yeshu = (int)(Math.Ceiling(count / 5.0));
// 計算數據庫一頁5條數據,能顯示多少頁
}
}
private static int yeshu; //構建總頁數類型 static 表示一直有這個值。
private void bindchinadata(int nowye,int numbers) //綁定數據函數①
nowye現在第幾頁,numbers每頁顯示數據的個數
{
Repeater1.DataSource = new ChinaStatesDA().Select(nowye,numbers);
Repeater1.DataBind();
TextBox1.Text = nowye.ToString();
// 調用DA中的查詢函數 select 綁定到Repeater 中
// TextBox1.Text 當前的頁數
}
protected void Button3_Click(object sender, EventArgs e) // 下一頁
{
int nowye = int.Parse(TextBox1.Text); // 構建當前頁面nowye 并賦值
if (yeshu!= nowye)
// 判斷 當前頁面 nowye 是否等于總頁數 yeshu , 不等于 從新綁定Repeater
{
bindchinadata(nowye + 1, 5);
}
}
protected void Button2_Click(object sender, EventArgs e) // 上一頁
{
int nowye = int.Parse(TextBox1.Text); // 構建當前頁面nowye 并賦值
if (nowye != 1)
// 判斷 當前頁面 nowye 是否等于第一頁 , 不等于 從新綁定Repeater
{
bindchinadata(nowye - 1, 5);
}
}
protected void Button5_Click(object sender, EventArgs e) // 跳轉頁面
{
int nowye = int.Parse(TextBox1.Text); // 構建當前頁面nowye 并賦值
if(nowye>0 && nowye<=yeshu) //判斷大于0小于總頁數從新綁定Repeater
{
bindchinadata(nowye, 5);
}
}
protected void Button1_Click(object sender, EventArgs e) //首頁
{
bindchinadata(1, 5);
}
protected void Button4_Click(object sender, EventArgs e) //末頁
{
bindchinadata(yeshu, 5);
}
}
|
新聞熱點
疑難解答