加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 何时填充已排序的asp:GridView?

发布时间:2020-12-16 07:39:53 所属栏目:asp.Net 来源:网络整理
导读:我试图通过多种方式提出这个问题.这是一个难以回答的问题,因为你必须了解正在发生的事情. 我什么时候填写GridView? 答案是在Page_Load期间,如果不是PostBack: protected void Page_Load(object sender,EventArgs e){ if (!IsPostBack) { DataSet ds = GetS
我试图通过多种方式提出这个问题.这是一个难以回答的问题,因为你必须了解正在发生的事情.

我什么时候填写GridView?

答案是在Page_Load期间,如果不是PostBack:

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

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

所以不要看IsPostBack

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

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

问题是,如果用户对列进行排序,则会在Page_Init和Page_Load之后调用OnSorting事件:

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

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

缓存适用于列排序

如果我愿意在列排序期间接受无效缓存,我可以将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();
}

害怕未知

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

但我相信我有义务将GridView返回到最后一次呈现页面时的状态.我必须在Page_Load之前(即在Page_Init期间)执行此操作. i have this fear because someone said so.所以我把它变成了

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

       StoreTheDatasetInTheSessionSomehowInCaseTheyCallSortInTheFuture(ds);

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

这个问题是GetStuffToShow依赖于用户在文本框中输入的内容,而这些内容在Page_Init期间不存在

无论如何,我在漫无边际.这里太热了.希望这个问题能得到解答,不像我的other recent frustrations与asp.net

奖金阅读

> Sorting GridView Formed With Data Set

解决方法

通过添加几个隐藏字段,一个用于排序表达式,另一个用于排序方向,您可以使用这些值在页面加载时填充GridView一次,然后更新排序事件中的排序(从All修改的排序代码) -In-One代码框架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();
    }

请注意,SortDirection和SortExpression是隐藏字段.这也很适合缓存DataSet.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读