I have a series of dropdownlists that I need to be able to reset with a button. I'm using tableadapters to populate the lists, with the following code to insert headers at the beginning of each list (the values that I need things to be reset to): (I call this onprerender of all the ddl's, as you can probably gather from the name)
1 // This is a preloading method that sets the headers on the dropdownlists
2 public void DropDownLists_PreRender(object sender, EventArgs e)
3 {
4 // local variables, set header to blank so ASP.NET doesn't yell at us.
5 DropDownList droplist;
6 droplist = (DropDownList)sender;
7 string header = "";
8
9 // switch on the ID of the dropdownlist that sent the postback
10 switch (droplist.ID.ToString())
11 {
12 case "DropDownList1": header = "Platform"; break;
13 case "DropDownList2": header = "Environment"; break;
14 case "DropDownList3": header = "Major Ver"; break;
15 case "DropDownList4": header = "Minor Ver"; break;
16 case "DropDownList5": header = "Physical Server"; break;
17 case "DropDownList6": header = "Instance"; break;
18 case "DropDownList7": header = "DBAS N"; break;
19 case "DropDownList8": header = "OS Version"; break;
20 }
21
22 // if the list is already populated and we haven't already inserted
23 // the header, do it now.
24 if (droplist.Items.Count > 0 && droplist.Items[0].Text != header)
25 {
26 droplist.Items.Insert(0, new ListItem(header, ""));
27 }
28
29 // if we've finished all the inserting, update the page count
30 // this can NOT be placed inside the switch, or the OS Version
31 // won't list properly on load.
32 if (droplist.ID.ToString() == "DropDownList8")
33 {
34 UpdatePageCount();
35 }
36
37 }
This code works fine for setting headers of the lists. The problem isn't in the switch, I've done it with 8 if loops too, this is just where I'm at with it right now. What I need is to reset the lists to that first value with a button, so I'm using this simple code:
1 // This is the clear filters button. Reset all search parameters
2 protected void Button1_Click(object sender, EventArgs e)
3 {
4 DropDownList1.ClearSelection();
5 DropDownList2.ClearSelection();
6 DropDownList3.ClearSelection();
7 DropDownList4.ClearSelection();
8 DropDownList5.ClearSelection();
9 DropDownList6.ClearSelection();
10 DropDownList7.ClearSelection();
11 DropDownList8.ClearSelection();
12
13 DropDownList1.SelectedIndex = 0;
14 DropDownList2.SelectedIndex = 0;
15 DropDownList3.SelectedIndex = 0;
16 DropDownList4.SelectedIndex = 0;
17 DropDownList5.SelectedIndex = 0;
18 DropDownList6.SelectedIndex = 0;
19 DropDownList7.SelectedIndex = 0;
20 DropDownList8.SelectedIndex = 0;
21 /* set by finding the text value. This sets it to the 1st one too!
22 DropDownList8.SelectedIndex = DropDownList8.Items.IndexOf(DropDownList8.Items.FindByText("OS Version"));
23 */
24 }
As you can see I've tried setting them the commented way as well, which resets most of them but doesn't solve the problem. So here's the odd behavior I'm getting:
Say I set the "Physical Server" dropdownlist and then need to clear the filters. When I push the clear filters button, all the lists will be reset to their starting point except for the Instance dropdownlist, which will be reset to the first value in the list (not the header). If I set multiple dropdownlists, it will clear all of them, but the next dropdown after the last one that was set will always be at the first value in the list instead of at the header that I'm putting in. (The header is still there, it's just set to the next value instead of to that one.)
Now I'm thinking this is a problem with the code above and not with the declarations of the data sources or with the lists because all the data in the lists is right, it's just not getting reset to the correct value. I think maybe there are two '0' values in the list - one set dynamically from the data source and one set by me when I add the header. That doesn't really explain the behavior that I'm getting though.
To Clarify, suppose here are my dropdownlists:
Platform
- Platform
- SQLServer
- UDB
- Oracle
Environment
- Environment
- Production
- Development
- Stage
- Lab
If I set the Platform List to any value, when I click the reset button Platform will be reset to Platform, but Environment will invariably be set to "Production" instead of the header item of "Environment". This is the case for all the lists in the set.