我在 C# .ASP 2010 中动态创建的 gridview 中启用分页时遇到问题。
protected void ButtonViewFiles_Click(object sender, EventArgs e)
{
ClearPanelConfigure();
/*
logic to populate variable "gridviewlist" which is a type of List<string>
...
*/
gv = new GridView();
gv.ShowHeader = false;
gv.BorderWidth = 0;
gv.ForeColor = CONTROL_FONT_COLOR;
gv.PageSize = 10;
gv.RowDataBound += new GridViewRowEventHandler(GridView_OnRowDataBound);
gv.AllowPaging = true;
gv.PageIndexChanging += new GridViewPageEventHandler(GridView_PageIndexChanging);
if (gridviewlist.Count > 0)
{
gv.DataSource = gridviewlist;
gv.DataBind();
PanelConfigure.Controls.Add(gv);
mySession.SaveSessionObject(gridviewlist, "GridViewList");
}
}
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
// Enable HTML decode for each cell in the gridview rows.
protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int c = 0; c < e.Row.Cells.Count; c++)
{
e.Row.Cells[c].Text = Server.HtmlDecode(e.Row.Cells[c].Text);
}
}
}
与大多数人在设计时拖放 GridView 控件不同,在我的例子中,Gridview 必须在运行时以编程方式创建。这就是为什么我有“gv = new GridView();” 换句话说,如果用户没有单击按钮并触发按钮事件,我将不会显示带有文件的所有 URL 链接的 GridView。
一旦创建了“gv”并启用了分页,gv 就会添加到 PanelConfigure(这是 Panel 的类型)。然后用户将能够查看所有文件链接。
“gv”是一个全局变量,否则“gv”将无法编译
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
对于在设计时拖放 GridView 的人来说,这不是问题。因为“gv”是用全局范围定义的。
如果我没有在“gv”中启用分页。Web 应用程序至少可以一次性运行并显示所有文件。但是,如果我在运行时使用“gv.AllowPaging = true;”启用分页,则会引发指向“gv.DataBind();”的异常 在按钮事件处理函数中
protected void ButtonViewFiles_Click(object sender, EventArgs e)
{
...
gv.DataBind();
...
}
异常说: NullReferenceException 未被用户代码处理。你调用的对象是空的。
System.NullReferenceException 未被用户代码处理
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.Web
StackTrace:
at System.Web.UI.WebControls.GridView.get_StateFormatter()
at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)
at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean addFirstLastPageButtons)
at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
at DataSearcher.advanced.ButtonViewFiles_Click(Object sender, EventArgs e) in C:\Users\fgui\documents\visual studio 2010\Projects\DataSearcher\DataSearcher\Advanced.aspx.cs:line 1853
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我搜索了这个网站。
上面的帖子没有提供动态创建的 GridView 的答案。它仅适用于设计时的 GridView 拖放。我对 GridView 不太熟悉。所以任何帮助表示赞赏。
谢谢。