Save selected item to DropDownList inside GridView after deleting some ListItems

I have a GridView with a DropDownList on each row. (The items in the DropDownList are the same for everyone.) I have a DropDownList "ddlView" outside of the GridView, which is used to filter the available options in other DropDownLists. The default selection for ddlView is not a filter.

When the user selects a new value for ddlView, any selected values โ€‹โ€‹in other DropDownLists disappear if they are not one of the values โ€‹โ€‹present after applying the filter. In this case, I would like the previous selected value to be still present and selected.

What is the best way to do this?

The previously selected values โ€‹โ€‹are available during the postback, but seem cleared after calling DataBind () in the GridView, so I can not determine their previous value in the method in which they are filled (RowDataBound event).

My best idea is to manually store this information in an object or collection during postback and reference it later during data binding events.

Is there a better way?

+4
source share
4 answers

I donโ€™t think there is a better way to do this, as when the GridView is bound, all the controls are recreated, thereby eliminating the selection.

The following works: (I keep the postback selection for retransmission in the RowDataBound event)

Markup

<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> 

code

 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) { } } } } 

Hope this helps.

+1
source

Instead of using a DataBinding to apply a filter, you can add / remove DropdownList elements using the Items property when another filter is selected. Thus, the selected value in the drop-down menus should not be reset.

0
source

You may be able to accomplish this with a simple condition. I donโ€™t know how you filter the elements, but it will look something like this:

 for (int itemIndex = 0; itemIndex < DropDownList1.Items.Count; itemIndex++) { ListItem item = DropDownList1.Items[itemIndex]; if (DropDownList1.Items.IndexOf(item) > ddlView.SelectedIndex) { if (!item.Selected) { DropDownList1.Items.Remove(item); } } } 

Not sure if this is what you are looking for, but hope this helps.

0
source

Use client side javascript

Find all the relevant selection inputs and for each one found, view the parameters and delete the selected one in the main ddl

 function UpdateDropDowns (tbl) { var controls = tbl.getElementsByTagName('select'); for (var i = 0; i < controls.length; i++) { RemoveOption(controls[i], 'the value to remove'); } } function RemoveOption(ddl, val) { for (var i = ddl.options.length; i>=0; i--) { if (ddl.options[i].value == val) { ddl.remove(i); } } } 
0
source

All Articles