我用以下代碼進行datalist的分頁,為何一翻頁時datalist就不見了?
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["ExamPaperDB"]);
SqlCommand cmd=new SqlCommand(query.ToString(),con);
SqlDataAdapter adp=new SqlDataAdapter(cmd);
dt=new DataTable();
adp.Fill(dt);
if(dt.Rows.Count!=0)
{
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dt.DefaultView;
objPage.AllowPaging = true;
objPage.PageSize = 6;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPage.CurrentPageIndex = CurPage-1;
lblCurPage.Text = "當前頁:第" + CurPage.ToString()+"頁";
if (!objPage.IsFirstPage)
//定義"上一頁"超級連結的URL為:當前執行頁面的虛擬路徑,並傳遞下一頁面的索引值
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
//如果當前頁面不是最後一頁
if (!objPage.IsLastPage)
//定義"下一頁"超級連結的URL為:當前執行頁面的虛擬路徑,並傳遞下一頁面的索引值
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);
dlResultList.DataSource=objPage;
dlResultList.DataBind();
答案一沒有重新綁定吧
答案二在Datalist的ItemCommand中需要重新綁定