Recently, I came across a problem with the CascadingDropDown control within the ASP.NET AJAX Control Toolkit. The question I had was simply this: How do I set the selected value of child DropDownList control?
The quick answer from Microsoft and others is to set the SelectedValue property of the CascadingDropDown control to the value you want selected. This works for the top-level parent DropDownList control, but since the child controls rely on the selected value of the parent on the client side, your server-side setting will be overwritten.
Several solutions have been posted over the last couple of years, most of which instruct you to use the CascadingDropDownProperties control to set the selected value of the target control. However, these properties were removed when Microsoft’s “Atlas” was converted to “ASP.NET AJAX.”
The solution I created used a combination of the List collection’s isDefaultValue and the ContextKey property of the child CascadingDropDown control. As the child menu’s list items are being gathered, I compare the current DataRow value with the ContextKey value. If they match, then I set isDefaultValue to true (otherwise false).
{
values.Add(new CascadingDropDownNameValue((string)dr["AttributeDefaultValueName"], dr["AttributeID"].ToString()));
if ((string)dr["AttributeDefaultValueName"] == contextKey)
{
values[valueCount].isDefaultValue = true;
}
else
{
values[valueCount].isDefaultValue = false;
}
valueCount++;
}
Programmatically, I set the selected values of the parent and child list by using the SelectedValue property of the parent and the ContextKey property of the child.
cascadingDropDownPackagingValues.ContextKey = childSelectedValue;
The other trick I needed to implement was clearing these controls on page load. I cleared both the parent and child DropDownLists, their selected values, and the child’s ContextKey:
ddlChild.Items.Clear();
cascadingDropDownParent.SelectedValue = “”;
cascadingDropDownChild.SelectedValue = “”;
cascadingDropDownParent.ContextKey = “”;
cascadingDropDownChild.ContextKey = “”;
I needed to do this because the previous values were being retained if I altered the selection and performed a post back (for example, if I changed the selection and fired the save event).
Feel free to leave a comment here if you are interested in sample of the entire solution (which includes the service’s Web methods, and the interactive Web form).
April 23, 2008
Posted in

content rss

Recent Comments