我正在以编程方式在Asp.net Gridview上创建分页。我收到错误“Microsoft JScript运行时错误:'grdAssetList_PageIndexChanged'未定义”下面是我的代码,为什么错误发生我还不明白。请帮助我。
if (ds.Tables[0].Rows.Count != 0)
{
setPages();
int startRow = ((pageNumber - 1) * pageSize) + 1;
int endRow = startRow + pageSize - 1;
if (endRow > rowCount) endRow = rowCount;
lblNowShowing.Text = startRow.ToString("###,##0") + " to " + endRow.ToString("###,##0") + " of " + rowCount.ToString("###,##0");
lblPageNum.Text = "Page " + txtCurrentPageIndex.Value + " of " + pageCount.ToString();
}
protected void setPages(){
int pageCount = Convert.ToInt16(txtPageCount.Value);
int currentPageIndex = Convert.ToInt16(txtCurrentPageIndex.Value);
int startPageNum = 1;
startPageNum = currentPageIndex - 3;
if (startPageNum < 1) startPageNum = 1;
int endPageNum = startPageNum + 10;
if (endPageNum > pageCount) endPageNum = pageCount;
PlaceHolder place = PlaceHolder1;
place.Controls.Clear();
for (int i = startPageNum; i <= endPageNum; i++)
{
if (i == currentPageIndex){
Label lblPage = new Label();
lblPage.Text = currentPageIndex.ToString();
place.Controls.Add(lblPage);
}
else{
HtmlAnchor btn = new HtmlAnchor();
**btn.Attributes.Add("onclick", "grdAssetList_PageIndexChanged(" + i.ToString() + ")");** // Here i am getting error
btn.InnerText = i.ToString();
btn.Title = "Page " + i.ToString();
btn.HRef = "#";
place.Controls.Add(btn);
}
Label lbl = new Label();
lbl.Text = " ";
place.Controls.Add(lbl); }
}
on Grid allowpaging = true和OnPageIndexChanging =“grdAssetList_PageIndexChanged”
答案 0 :(得分:0)
您的代码需要客户端功能;不是服务器端。
设置allowpaging = false
将btn定义为LinkButton(不是Anchor)
更改您加注星号的代码行。
btn.ID= "btn" + i.ToString();
btn.Click += btn_Click;
现在将函数写为
private void btn_Click(object sender, EventArgs e)
{
int pageno = int.Parse(((LinkButton)sender).ID.Replace("btn",""));
LoadData(((pageno - 1) * gvPosts.PageSize) + 1, gvPosts.PageSize);
}