標籤:blog color os io ar for 資料 art 問題
public string SQL = "select * from Memorandum"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["SQL"] = SQL; BindData(); } if (Request.QueryString["MId"] != null) { Delete(int.Parse(Request.QueryString["MId"].ToString())); } } protected void AspNetPager1_PageChanged(object sender, EventArgs e) { BindData(); } void BindData() { PagedDataSource pds = new PagedDataSource(); IList<MemorandumInfo> Infos = new DAL.Memorandum().GetAllMemorandumsBySQL(ViewState["SQL"].ToString()); pds.DataSource = Infos; pds.AllowPaging = true; pds.PageSize = 10;//取控制項的分頁大小 pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//顯示當前頁 //設定控制項 this.AspNetPager1.RecordCount = Infos.Count;//記錄總數 this.AspNetPager1.PageSize = 10; Repeater1.DataSource = pds; Repeater1.DataBind(); } void Delete(int MId) { new Memorandum().DeleteMemorandum(MId); Response.Write("<script>alert(‘成功刪除該條備忘資訊‘);location=‘?‘;</script>"); } /// <summary> /// 查詢 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { if (DropDownList1.SelectedValue == "0") { ViewState["SQL"] = "select * from Memorandum"; BindData(); } else { ViewState["SQL"] = "SELECT MId, MemoTitle, MemoContent, MemoStartTime, MemoEndTime FROM Memorandum WHERE (MemoEndTime - NOW() <=7)"; BindData(); } } } |
ViewState 用來跟蹤和儲存控制項的狀態資訊。否則這些資訊可能會丟失,原因可能是這些值不隨著 form 回傳,或者根本就不在 page 的 html 中。
ViewState 中儲存著代碼中改變的控制項屬性,通過代碼綁定到控制項的任何資料,以及由使用者操作觸發,回傳的任何更改。
ViewState 還提供了一個狀態包(StateBag), 這是一個特殊的集合或字典(collection or dictionary), 可以用來儲存,通過一個 key 來恢複任意的對象或者值。
賦值:ViewState[key] = value;
取值:value = ViewState[key];
(轉)AspNetPager查詢分頁問題(點擊頁碼,不再是查詢後的資料集)viewstate解決