什么时候填写一个排序的asp:GridView?

Ian*_*oyd 5 asp.net gridview columnsorting

我试图通过多种方式提出这个问题.这是一个难以回答的问题,因为你必须了解正在发生的事情.

我什么时候填写GridView?


Nieve酒店答案时Page_Load,如果没有一个回发:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       DataSet ds = GetStuffToShow();
       GridView1.DataSource = ds;
       GridView1.DataBind();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在于,如果它回发,则网格不会被填充.网格未填充的原因是因为我们关闭了网格的视图状态.

所以不要看IsPostBack

我们需要始终填充网格,回发与否:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = GetStuffToShow();
    GridView1.DataSource = ds;
    GridView1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

但问题是,如果用户排序列,该OnSorting事件被称为两者Page_InitPage_Load:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataSet ds = GetStuffToShow(e.SortExpression, e.SortDirection);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}    
Run Code Online (Sandbox Code Playgroud)

我们运行两个数据库查询,只需要一个.

缓存适用于列排序

如果我愿意在列排序期间接受无效缓存,我可以将其存储DataSet在会话变量中,只要我为任何其他操作使其无效.

问题是我需要它之后OnSorting调用事件():Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (AGridViewOnSortingEventIsntBeingRaised)
    {
       DataSet ds = GetStuffToShow();

       StoreTheDatasetInTheSessionSomehowInCaseTheyCallSortInTheFuture(ds);

       GridView1.DataSource = ds;
       GridView1.DataBind();
    }
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataSet ds = GetDataSetOutOfSessionSomehowThatDamnWellBetterBeThere();

    SomehowSortAReadOnlyDisconnectedDataSet(ds, e.SortExpression, e.SortDirection);

    GridView1.DataSource = ds;
    GridView1.DataBind();
}    
Run Code Online (Sandbox Code Playgroud)

害怕未知

然后我仍然有恐怖,因为我关闭了GridView的视图状态.asp:GridView当我可以从服务器(或从内存)重建它时,我认为只读应该需要几十千字节的base64编码.

但我相信我有义务返回GridView到最后一次呈现页面时的状态.我必须在之前 Page_Load(即期间Page_Init)这样做.我有这种恐惧,因为有人这么说.所以我把它变成了

protected void Page_Init(object sender, EventArgs e)
{
    if (AGridViewOnSortingEventIsntBeingRaised)
    {
       DataSet ds = GetStuffToShow();

       StoreTheDatasetInTheSessionSomehowInCaseTheyCallSortInTheFuture(ds);

       GridView1.DataSource = ds;
       GridView1.DataBind();
    }
}
Run Code Online (Sandbox Code Playgroud)

这个问题GetStuffToShow取决于用户在文本框中键入的内容,这些文本框在此期间不存在Page_Init

无论如何,我在漫无边际.这里太热了.希望这个问题能得到解答,不像我最近 asp.net的其他 挫败感

奖金阅读

小智 0

通过添加几个隐藏字段,一个用于排序表达式,另一个用于排序方向,您可以使用这些值在页面加载时填充 GridView 一次,然后在排序事件中更新排序(从 All 修改的排序代码) - 一体化代码框架 GridView 示例):

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = GetStuffToShow();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // If the sorting column is the same as the previous one, 
        // then change the sort order.
        if (SortExpression.Value.Equals(e.SortExpression))
        {
            SortDirection.Value = SortDirection.Value.Equals("ASC") ? "DESC" : "ASC";
        }
        // If sorting column is another column, 
        // then specify the sort order to "Ascending".
        else
        {
            SortExpression.Value = e.SortExpression;
            SortDirection.Value = "ASC";
        }

        var sortedView = new DataView(<convert your DataSet to a DataTable>)
            { Sort = string.Format("{0} {1}", this.SortExpression.Value, this.SortDirection.Value) };
        GridView1.DataSource = sortedView;

        GridView1.DataBind();
    }
Run Code Online (Sandbox Code Playgroud)

请注意,SortDirection 和 SortExpression 是隐藏字段。这也非常适合缓存数据集。

另外,我不会担心您提出的 Page_Init 问题。这仅适用于动态创建控件的情况。