다음은 GridView 컨트롤을 채우는 방법입니다. 나는 .aspx 프론트 엔드가 아닌 코드 숨김에서 이것을 수행하고있다. 다음은 내가 무슨의 매우 축약 버전 :RowDataBound 이벤트의 한 줄이 Gridview의 호출 메커니즘과 충돌합니다
private void UpdateGridView()
{
DataTable temptable = new DataTable();
DataColumn idcol = new DataColumn();
DataColumn titlecol = new DataColumn();
idcol.ColumnName = "ID";
titlecol.ColumnName = "Title";
temptable.Columns.Add(idcol);
temptable.Columns.Add(titlecol);
...(get data from the database, store it as variable "x")...
DataRow tempdr;
tempdr[idcol] = x.ID;
tempdr[titlecol] = x.Title;
temptable.Rows.Add(tempdr);
GridView1.DataSource = temptable;
GridView1.DataBind();
}
의 GridView의 "AllowPaging이"true로 설정하고, 나는 다음과 같은 이벤트 핸들러가 페이징 처리하려면 :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
UpdateGridView();
}
을 그리고이 위대한 작품을 !
그러나 나는 또한 RowDataBound 이벤트 핸들러를 가지고 : 여기
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; //hide the ID
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'";
}
}
내 목표는 자신이 클릭 할 행을 가지고 있고, 그 행의 ID와 동일한 쿼리 문자열을 다른 페이지로 연결하는 것입니다. ID 열에 값을 입력해야 행을 만들 때 액세스 할 수 있으므로 링크의 QueryString에 ID를 추가 할 수 있습니다. 하지만 ID 열이 보이기를 원하지 않기 때문에 다음 줄을 추가했습니다. e.Row.Cells[0].Visible = false;
이렇게하면 페이징 기능이 중단됩니다. 페이지 번호가 더 이상 표시되지 않습니다. 이 한 줄을 주석으로 처리하면 모든 것이 작동하지만 ID가 GridView에 표시됩니다.
1) 이유는 무엇입니까? 2) 동일한 기능을 사용하기 위해 할 수있는 일은 무엇입니까?