asp.net – 何时填充已排序的asp:GridView?
我试图通过多种方式提出这个问题.这是一个难以回答的问题,因为你必须了解正在发生的事情.
我什么时候填写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问题.这应该仅适用于动态创建控件的情况. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET MVC与Webforms:替换WebForms控件
- ASP.NET中用gridView来实现分页
- asp.net-mvc – Full-Stack Web应用程序框架的定义是什么
- asp.net – 如何在web.config中迭代自定义键?
- asp.net-mvc – 命名空间’System.Web.Mvc’中不存在’Ajax
- asp.net – 如何使用多个项目部署单个解决方案?
- 在asp.net核心mvc中集成ckeditor
- ASP.NET httpRedirect:重定向所有页面,除了一个
- asp.net – 前端头还是后端?
- 如何显示或隐藏基于角色的控件 – ASP.NET MVC 4 Razor
- asp.net-mvc – 即使我设置CustomError =“On”,
- 在ASP.NET(C#)中创建动态RSS提要页面 – 我是否需
- asp.net – Dapper.net交易问题
- asp.net-mvc – ASP MVC – 有默认内容类型的任何
- asp.net – 如何将onchange事件附加到CheckBoxLi
- asp.net-mvc – 添加TwilioController基类继承后
- 经典的asp字符编码
- 哪个表现最好:经典ASP,ASP.NET WebForms还是ASP
- asp.net – 有没有办法禁用.NET浏览器检测?
- asp.net-mvc – 将带有字符串的json数组发布到As