我有以下代码将gridview绑定到包含分页的特定数据源,但是当我点击下一页时出现错误:
GridView'SPGridView1'触发了未处理的事件PageIndexChanging。
代码如下:
namespace AuditingLog.AuditLogViewerWebPart
{
public partial class AuditLogViewerWebPartUserControl : UserControl
{
protected string PageTitle;
protected string PageTitleInArea;
protected string PageDescription;
private SPList _HistorialIngresos;
public SPListItemCollection _lstRecords;
private SPWeb web;
protected void Page_Load(object sender, EventArgs e)
{
// only show delete button to site collection owner
cmdDeleteAllEntries.Visible = SPContext.Current.Web.CurrentUser.IsSiteAdmin;
tableButtons.Visible = SPContext.Current.Web.CurrentUser.IsSiteAdmin;
if (!Page.IsPostBack)
{
SetupWebpart();
SetupGrid();
BindGridToDataSource();
//GetDataFromDataSource();
}
}
private void BindGridToDataSource()
{
SPGridView1.DataSource = GetDataFromDataSource();
SPGridView1.DataBind();
}
private void SetupGrid()
{
SPBoundField boundField = new SPBoundField();
boundField.HeaderText = "User";
boundField.DataField = "User";
SPGridView1.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "Event";
boundField.DataField = "Event";
SPGridView1.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "ItemType";
boundField.DataField = "ItemType";
SPGridView1.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "DocLocation";
boundField.DataField = "DocLocation";
SPGridView1.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "Occurred";
boundField.DataField = "Occurred";
boundField.ControlStyle.Width = new Unit(120);
SPGridView1.Columns.Add(boundField);
SPGridView1.AutoGenerateColumns = false;
//SPGridView1.DataSource = GetDataFromDataSource();
// Turn on paging and add event handler
SPGridView1.PageSize = 3;
SPGridView1.AllowPaging = true;
SPGridView1.PageIndexChanging +=
new GridViewPageEventHandler(SPGridView1_PageIndexChanging);
SPGridView1.PagerTemplate = null; // Must be called after Controls.Add(oGrid)
//SPGridView1.DataBind();
SPGridView1.AllowSorting = true;
SPGridView1.HeaderStyle.Font.Bold = true;
}
/// <summary>
/// Get data from Sharepoint Audit Query API
/// </summary>
/// <returns></returns>
private DataTable GetDataFromDataSource()
{
DataTable table = new DataTable();
// read audit log
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteColl = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb site = siteColl.OpenWeb(SPContext.Current.Web.ID))
{
SPAuditQuery wssQuery = new SPAuditQuery(siteColl);
SPAuditEntryCollection auditCol = siteColl.Audit.GetEntries(wssQuery);
table.Columns.Add("User", typeof(string));
table.Columns.Add("Event", typeof(string));
table.Columns.Add("ItemType", typeof(string));
table.Columns.Add("DocLocation", typeof(string));
table.Columns.Add("Occurred", typeof(DateTime));
DataRow newRow;
foreach (SPAuditEntry entry in auditCol)
{
newRow = table.Rows.Add();
newRow["User"] = GetUserNameById(entry.UserId, site);
newRow["Event"] = entry.Event;
newRow["ItemType"] = entry.ItemType.ToString();
newRow["DocLocation"] = entry.DocLocation;
newRow["Occurred"] = entry.Occurred.ToLocalTime();
}
}
}
});
return table;
}
void SPGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SPGridView1.DataSource = GetDataFromDataSource();
SPGridView1.PageIndex = e.NewPageIndex;
SPGridView1.DataBind();
}
/// <summary>
/// Incializacion de controles
/// </summary>
protected void SetupWebpart()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (var site = new SPSite(SPContext.Current.Site.ID))
{
web = site.AllWebs["/intranet"];
_HistorialIngresos = web.Lists["HistorialIngresos"];
}
});
}
string GetUserNameById(int UserId, SPWeb site)
{
if (UserId == -1)
{
return @"SHAREPOINT\System";
}
else
{
return site.AllUsers.GetByID(UserId).Name;
}
}
protected void cmdDeleteAllEntries_Click(object sender, EventArgs e)
{
SPSite siteColl = SPContext.Current.Site;
siteColl.Audit.DeleteEntries(DateTime.Now.ToLocalTime().AddDays(1));
siteColl.Audit.Update();
Response.Redirect(Request.RawUrl);
}
protected void cmdRefreshPage_Click(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl);
}
}
}
aspx代码
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AuditLogViewerWebPartUserControl.ascx.cs" Inherits="AuditingLog.AuditLogViewerWebPart.AuditLogViewerWebPartUserControl" %>
<asp:Panel ID="paBotones" runat="server" Width="100%">
<style type="text/css">
h2
{
margin: 29px 0 10px 0 !important;
}
.80041300-55c7-4b95-891a-e065a9b72a97 th
{
font-size: 15px !important;
}
</style>
<table runat="server" id="tableButtons" style="width:100%; background-color: #CCCCCC;" cellpadding="2" cellspacing="0">
<tr>
<td>
<asp:Button ID="cmdRefreshPage" UseSubmitBehavior="false" runat="server" Text="Refresh Page" OnClick="cmdRefreshPage_Click" Width="100px" Font-Size="8pt" />
<asp:Button ID="cmdDeleteAllEntries" UseSubmitBehavior="false" runat="server" Text="Clear Audit Log" OnClick="cmdDeleteAllEntries_Click" Width="100px" Font-Size="8pt" />
</td>
</tr>
</table>
<SharePoint:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="white" AlternatingRowStyle-BackColor="Transparent" Width="100%" Height="220px">
<AlternatingRowStyle CssClass="ms-alternating" />
</SharePoint:SPGridView>
</asp:Panel>
答案 0 :(得分:0)
SPGridView1_PageIndexChanging
应为protected
,因为您AutoEventWireup="true"
同时从代码隐藏中删除事件订阅。