asp.net – 删除一些ListItem后,在GridView中的DropDownList中维
发布时间:2020-12-16 09:35:31 所属栏目:asp.Net 来源:网络整理
导读:我有一个GridView,每行都有一个DropDownList. (DropDownList中的项目对于每个项目都是相同的.)我在GridView之外有一个DropDownList“ddlView”,用于过滤其他DropDownLists中的可用选项. ddlView的默认选择是没有过滤器. 当用户为ddlView选择新值时,如果它们
我有一个GridView,每行都有一个DropDownList. (DropDownList中的项目对于每个项目都是相同的.)我在GridView之外有一个DropDownList“ddlView”,用于过滤其他DropDownLists中的可用选项. ddlView的默认选择是没有过滤器.
当用户为ddlView选择新值时,如果它们不是应用过滤器后存在的值之一,则其他DropDownLists中的任何选定值都将消失.在这种情况下我想要发生的是先前选择的值仍然存在并被选中. 完成此任务的最佳方法是什么? 之前选择的值在回发期间可用,但在GridView上调用DataBind()后似乎会被清除,因此我无法在填充它们的方法中确定它们之前的值(RowDataBound事件). 到目前为止,我最好的想法是在回发期间手动将该信息存储到对象或集合中,并在数据绑定事件期间稍后引用它. 有没有更好的办法? 解决方法
我不认为有更好的方法来实现这一点,因为当GridView被绑定时,所有控件都被重新创建,从而删除了选择.
以下工作原理:(我将选择存储在postback上,以便在RowDataBound事件中再次检索) 标记 <asp:Button ID="button1" runat="server" Text="Post Back" OnClick="button1_Click" /> <br /> <asp:GridView ID="gridView1" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="gridViewDropDownList" runat="server"> <asp:ListItem Value="1">Item 1</asp:ListItem> <asp:ListItem Value="2">Item 2</asp:ListItem> <asp:ListItem Value="3">Item 3</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 码 public class GridViewDropDownSelections { public int RowIndex { get; set; } public int SelectedIndex { get; set; } } ... private List<GridViewDropDownSelections> selectedDropDownListItems = new List<GridViewDropDownSelections>(); protected override void OnLoad(EventArgs e) { var selections = gridView1.Rows.Cast<GridViewRow>().Where(r => r.RowType == DataControlRowType.DataRow) .Select(r => new GridViewDropDownSelections() { RowIndex = r.RowIndex,SelectedIndex = ((DropDownList)r.FindControl("gridViewDropDownList")).SelectedIndex }).ToList(); selectedDropDownListItems.AddRange(selections); gridView1.RowDataBound += new GridViewRowEventHandler(gridView1_RowDataBound); if (!IsPostBack) { BindDataGrid(); } base.OnLoad(e); } protected void button1_Click(object sender,EventArgs e) { BindDataGrid(); } private void BindDataGrid() { //Dummy data string[] data = new string[] { "Item 1","Item 2","Item 3" }; gridView1.DataSource = data; gridView1.DataBind(); } void gridView1_RowDataBound(object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var selection = selectedDropDownListItems.FirstOrDefault(i => i.RowIndex == e.Row.RowIndex); if (selection != null) { try { DropDownList gridViewDropDownList = (DropDownList)e.Row.FindControl("gridViewDropDownList"); gridViewDropDownList.SelectedIndex = selection.SelectedIndex; } catch (Exception) { } } } } 希望能帮助到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 强制ASP.Net MVC Bundle以某种顺序呈现javascript文件
- asp.net – ApplicationInstance.CompleteRequest不会停止执
- 构建高性能ASP.NET应用的几点建议
- asp.net-mvc-3 – 在使用Unity容器时为此对象异常定义的无参
- asp.net – 请求在IIS工作进程中存在于RequestAcquireState
- 如何将ASP.NET用户控件转换为Web /复合控件?
- asp.net – Azure上的联合身份验证
- ASP.NET Core 认证与授权[6]:授权策略是怎么执行的?
- asp.net – 将日期字符串转换为DateTime格式vb.net
- asp.net-mvc – 我的bin部署MVC4应用程序中的区域路由有什么
推荐文章
站长推荐
- asp.net-mvc-3 – MVC3,Ninject和Ninject.MVC3问
- asp.net-mvc – 如何使用Asp.net MVC验证列表属性
- asp.net-mvc-3 – 如何在MVC3上使用authorize属性
- asp.net – 显示和隐藏转发器中的特定列?
- asp.net-mvc – 你可以使用Visual Studios代替We
- asp.net-identity – IIdentity.Name与IIdentity
- .Net 5分钟搞定网页实时监控
- c#中@标志的作用
- asp.net-mvc-3 – 如何调试AutoMapper“缺少类型
- asp.net-mvc-3 – 什么是MVC 3中的ModelState类?
热点阅读