【问题标题】:Sorting GridView on code behind. Gridview generated dynamically. OnSorting method not been called在后面的代码上对 GridView 进行排序。 Gridview 动态生成。未调用 OnSorting 方法
【发布时间】:2017-04-10 06:28:30
【问题描述】:

我需要创建很多 GridViews 以显示在 aspx 页面上。

然后,我创建了一个方法来生成这些GridViews,基于名称数组和DataTables。

反正每个GridView的生成方式如下:

GenerateGridView(string gvName, DataTable dtGridView){
GridView Gview = new GridView();
Gview.ID = gvName;
Gview.AutoGenerateColumns = true;
Gview.CssClass = "table table-responsive table-condensed table-striped table-bordered";
Gview.CellPadding = 4;
Gview.GridLines = 0;
Gview.AllowPaging = false;
Gview.Attributes.Add("Font-Size", "Smaller");
Gview.Attributes.Add("HeaderStyle-Font-Size", "Small");
Gview.AllowSorting = true;
}

问题:我需要OnSorting 方法。但是我没有GridViews 的所有名称,它们将动态生成(Gvied.ID 或 gridview ID 是根据 SELECT 命令中的表名动态生成的)。

所以我无法创建protected void gridViewName_OnSorting 方法。

Gridview 生成平滑。但是每次我点击一个标题,我都会得到一个

System.Web.HttpException

排序异常。

我创建了一个通用排序:

protected void gvSorting(object sender, GridViewSortEventArgs e) 

然后我给GenerateGridview(..)中的所有GridViews添加了一个属性:

Gview.Attributes.Add("OnSorting", "gvSorting");

但是,我不断收到 Http 排序异常。我调试了代码,除此之外

OnSorting = gvSorting

出现在生成的GridViews 上,该错误依然存在。

【问题讨论】:

    标签: c# asp.net sorting exception gridview


    【解决方案1】:

    您所要做的就是将Sorting 方法添加到GenerateGridView 中的GridView 中

    Gview.Sorting += GridViewAll_Sorting;
    

    并为所有的 GridViews 创建一个排序方法

    protected void GridViewAll_Sorting(object sender, GridViewSortEventArgs e)
    {
        //cast the sender as a gridview
        GridView Gview = sender as GridView;
    
        //get the datatable from viewstate (or another source)
        DataTable dt = ViewState["dtGridView"] as DataTable;
    
        //sort the datatable
        dtGridView.DefaultView.Sort = e.SortExpression;
    
        //bind the data to the gridview
        Gview.DataSource = dtGridView;
        Gview.DataBind();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多