简体   繁体   English

使用C#.ASP中的动态/编程创建的网格视图启用分页

[英]Enable Paging with Dynamically/Programmatically created Grid View in C# .ASP

I have a problem in enabling paging in a dynamically created gridview in C# .ASP 2010. 我在C#.ASP 2010中动态创建的网格视图中启用分页时遇到问题。

  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);
        }
     }
  }

Unlike most people drag and drop the GridView control at design time, the Gridview in my case has to be programmatically created at the run time. 与大多数人在设计时拖放GridView控件不同,我的情况下的Gridview必须在运行时以编程方式创建。 That is why I have "gv = new GridView();" 这就是为什么我有“gv = new GridView();” In other words, if user does not click a button and fires the button event, I will not show the GridView with all the URL links to files. 换句话说,如果用户没有单击按钮并触发按钮事件,我将不会向GridView显示文件的所有URL链接。

Once "gv" is created and enabled with paging, gv is added to PanelConfigure (which is type of Panel). 创建“gv”并使用分页启用后,gv将添加到PanelConfigure(Panel类型)。 Then user will be able to view all the file links. 然后用户将能够查看所有文件链接。

"gv" is a global variable, otherwise "gv" will not compile in “gv”是一个全局变量,否则“gv”将无法编译

  protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {        
     gv.PageIndex = e.NewPageIndex;
     gv.DataBind();
  }

For people with drag and drop GridView at design time, this is not a problem. 对于在设计时拖放GridView的人来说,这不是问题。 Because "gv" is defined with global scope. 因为“gv”是用全局范围定义的。

If I don't enable paging in "gv". 如果我不在“gv”中启用分页。 The web application at least runs and shows all the files in one shot. Web应用程序至少运行并一次性显示所有文件。 However, if I enable paging at run time with "gv.AllowPaging = true;", exception is thrown which points to "gv.DataBind();" 但是,如果我在运行时使用“gv.AllowPaging = true;”启用分页,则抛出异常,指向“gv.DataBind();” in button event handler function 在按钮事件处理函数中

  protected void ButtonViewFiles_Click(object sender, EventArgs e)
  {        
     ...

     gv.DataBind();

     ...
  }

The exception says: NullReferenceException was unhandled by user code. 例外情况说:NullReferenceException未被用户代码处理。 Object reference not set to an instance of an object. 你调用的对象是空的。

System.NullReferenceException was unhandled by user code 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: 

I searched this web site. 我搜索了这个网站。

how to create paging in a dynamic gridview 如何在动态网格视图中创建分页

The above post does not provide an answer to dynamically created GridView. 上面的帖子没有提供动态创建的GridView的答案。 It only works for GridView drag and drop at design time. 它仅适用于GridView在设计时拖放。 I am not quite familiar with GridView. 我对GridView不太熟悉。 So any help is appreciated. 所以任何帮助都表示赞赏。

Thanks. 谢谢。

Creating such complicated controls dynamically and maintaining their state during postbacks could be extemely difficult. 动态创建这样复杂的控件并在回发期间维持其状态可能非常困难。 I really suggest that instead you create the control statically in the declarative code and only sets its Visible to true/false when necessary. 我真的建议您在声明性代码中静态创建控件,并在必要时仅将其Visible设置为true / false。

A dynamically added control will last for the current request only. 动态添加的控件将仅持续当前请求。 If you use paging or any other action (!) on this page , a postback will be send to the server. 如果您在此页面上使用分页或任何其他操作(!),则会将回发发送到服务器。 The new request will recreate the page-object again without creating the dynamically added control. 新请求将再次重新创建页面对象,而不创建动态添加的控件。 This behaviour is by purpose, because a postback request might be sent to another server in the server farm. 此行为是有目的的,因为回发请求可能会发送到服务器场中的另一个服务器。 Since you do not have stored the existence-information about the gridview somewhere else (eg in the ViewState), the server is not able to reconstruct it on a new request. 由于您没有在其他地方存储有关gridview的存在信息(例如,在ViewState中),因此服务器无法在新请求上重建它。

In conclusion, you will have to maintain the state and reconstruction of the control by yourself (very tricky if you are not familiar with page cycle, view state etc. ). 总之,您必须自己维护控件的状态和重建(如果您不熟悉页面循环,查看状态等,则非常棘手)。 Alternativly you follow Wiktor and create the grid view statically in the declarative code and use the visibility flag (highly recommended). 另外,您遵循Wiktor并在声明性代码中静态创建网格视图,并使用可见性标记(强烈推荐)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM