简体   繁体   中英

Prepopulate multiple cascading dropdown lists in C# ASP

I am writing an edit page in C#/ASP and have a set of cascading dropdown lists that already have values in the database that I need to pre-select for the user. On the page itself, I'm already using:

<asp:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="itemMCat" Category="itemMCat" PromptText="Select a Main Category" ServicePath="categoryService.asmx" ServiceMethod="getMCat"></asp:CascadingDropDown>
<asp:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="itemCat" ParentControlID="itemMCat" PromptText="Please select a Category" ServiceMethod="getCat" ServicePath="categoryService.asmx" Category="itemCat"></asp:CascadingDropDown>
<asp:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="itemSCat" ParentControlID="itemCat" PromptText="Please select a Sub Category" ServiceMethod="getSubCat" ServicePath="categoryService.asmx" Category="itemSCat"></asp:CascadingDropDown>    

to operate the lists. However, because of this, it forces the user to have to re-select the values (since they were selected previously). This is an issue for me because it is an edit page, and I only want to have them change what needs changed, and not reselect everything.

In the back end, I am selecting the data and calling functions to populate the lists (which at first, only attempted to set the dropdown to the value in the database, but that didn't work either since the code behind runs before the page loads [so before the code up top even ran]). This code does work, but only if the above code is taken out. If it is not taken out, this code has no effect.

Dictionary<string, string> catagoryMList;
catagoryMList = CowansPublic.getMCat();

Dictionary<string, string> catagoryList;
catagoryList = CowansPublic.getCat(mainCategory);

Dictionary<string, string> catagorySList;
catagorySList = CowansPublic.getSubCat(category);

itemMCat.DataSource = catagoryMList;
itemMCat.DataTextField = "Value";
itemMCat.DataValueField = "Key";
itemMCat.DataBind();
itemMCat.SelectedValue = mainCategory;

itemCat.DataSource = catagoryList;
itemCat.DataTextField = "Value";
itemCat.DataValueField = "Key";
itemCat.DataBind();
itemCat.SelectedValue = category;

itemSCat.DataSource = catagorySList;
itemSCat.DataTextField = "Value";
itemSCat.DataValueField = "Key";
itemSCat.DataBind();
itemSCat.SelectedValue = subCategory;

So how would I keep the cascading dropdown effect, but have all 3 dropdowns already populated with the saved information? Thanks in advance.

** EDIT: in response to the comments**

@Nelson Reis: What I mean about it only works if the top code is taken out is that I cannot pre-select the options that the user had selected (according to the data in the database) by putting .selectedValue code in the code-behind page.

@Arthur P: Yes, DDL1 is the only one you can do anything with until you select something in it, which activates DDL2, and so on. But since this is the edit page, I want them all active with the current selections already made. Taking the top portion of code out, my code-behind does this. But then it doesn't work as a cascading drop-down anymore.

You have to set the SelectedValue properties of the cascading dropdown extenders themselves, not the ones of their target controls:

CascadingDropDown1.SelectedValue = mainCategory;
CascadingDropDown2.SelectedValue = category;
CascadingDropDown3.SelectedValue = subCategory;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM