当我单击页面时,分页不起作用。什么都没有显示。这是代码,
我的 ascx 表单上的标记,
<div class="mGrid">
<SharePoint:SPGridView
runat="server"
ID="gdSharedReport"
AutoGenerateColumns="false"
CssClass="mGrid"
AllowPaging="true"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
EmptyDataText="No Survey found."
OnSorting="gdSharedReport_Sorting"
OnPageIndexChanging="gdSharedReport_PageIndexChanging"
/>
</div>
我的 ascx.cs 上的代码,
public DataView dv;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateGrid();
}
}
private DataTable BindData()
{
DataTable table;
table = new DataTable();
table.Columns.Add("SurveySet", typeof(string));
table.Columns.Add("SurveySection", typeof(string));
table.Columns.Add("SurveyQuestion", typeof(string));
table.Columns.Add("Employee", typeof(string));
table.Columns.Add("Supervisor", typeof(string));
table.Columns.Add("EmployeeNumber", typeof(string));
table.Columns.Add("SurveyDate", typeof(string));
DataRow row;
try
{
SPListItemCollection collListItems = list.GetItems(oQuery);
row = table.NewRow();
//Logic here to set rows
table.Rows.Add(row);
}
catch(Exception ex){}
return table;
}
private void GenerateGrid()
{
DataTable dt = BindData();
dv = new DataView(dt);
gdSharedReport.DataSource = dv;
gdSharedReport.AutoGenerateColumns = false;
gdSharedReport.AllowSorting = true;
gdSharedReport.Sorting += new GridViewSortEventHandler(gdSharedReport_Sorting);
//Setting bound fields here
gdSharedReport.PageSize = 10;
gdSharedReport.AllowPaging = true;
gdSharedReport.PageIndexChanging +=
new GridViewPageEventHandler(gdSharedReport_PageIndexChanging);
gdSharedReport.PagerTemplate = null;
if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
{
dv.Sort = ViewState["SortExpression"].ToString()
+ " " + ViewState["SortDirection"].ToString();
}
gdSharedReport.DataBind();
}
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataBind();
}
public void gdSharedReport_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = "asc";
if (e.SortExpression == lastExpression)
newDirection = (lastDirection == "asc") ? "desc" : "asc";
ViewState["SortExpression"] = e.SortExpression;
ViewState["SortDirection"] = newDirection;
dv.Sort = e.SortExpression + " " + newDirection;
gdSharedReport.DataBind();
}
这是网格加载的方式,
这是我点击页面时的结果,
如果我修改以下功能,
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataBind();
}
至,
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataSource = BindData();
gdSharedReport.DataBind();
}
然后分页开始工作,但如果我点击分页,分页号码就会消失,