I have a cascading drop down menu that includes 2 drop downs with a "OnSelectedIndexChanged" event on the second drop down to display my data. the items in both drop downs show up fine but my "OnSelectedIndexchanged" does not fire the first time I make a selection on my second drop down, but if I select another item, it will fire then. Can someone help with this?
webservice code:
[WebMethod]
public CascadingDropDownNameValue[] GetCategories(string knownCategoryValues)
{
string query = "SELECT 'ALL' AS CategoryName, 1 AS CategoryID UNION SELECT CategoryName, CategoryID FROM flCategories WHERE ParentID IS NULL";
List<CascadingDropDownNameValue> categories = GetData(query);
return categories.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetSubCategories(string knownCategoryValues)
{
string query;
string categories = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["CategoryID"];
if (categories == "1")
{
query = string.Format("SELECT 'Select SubCategory' AS CategoryName, 0 AS CategoryID UNION SELECT 'ALL' AS CategoryName, 1 AS CategoryID UNION SELECT CategoryName, CategoryID FROM flCategories WHERE ParentID IS NOT NULL", categories);
}
else if (categories == "0")
{
query = string.Format("SELECT CategoryName, CategoryID FROM flCategories WHERE ParentID = {0}", categories);
}
else
{
query = string.Format("SELECT 'Select SubCategory' AS CategoryName, 0 AS CategoryID UNION SELECT 'ALL' AS CategoryName, 1 AS CategoryID UNION SELECT CategoryName, CategoryID FROM flCategories WHERE ParentID = {0}", categories);
}
List<CascadingDropDownNameValue> subcategories = GetData(query);
return subcategories.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[1].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
aspx code:
<label for="recipient">CATEGORY</label>
<asp:DropDownList ID="categoryList0"
runat="server"
Height="20px"
Width="181px">
</asp:DropDownList>
<Ajax:CascadingDropDown ID="cddCategories"
TargetControlID="categoryList0"
PromptText="Select Category"
PromptValue="0"
ServicePath="~/ServiceCS.asmx"
ServiceMethod="GetCategories"
runat="server"
Category="CategoryID"
LoadingText="Loading..." />
<label for="recipient">SUBCATEGORY</label>
<asp:DropDownList ID="subcategoryList0"
runat="server"
DataTextField="CategoryName"
DataValueField="CategoryID"
Height="20px"
OnSelectedIndexChanged="subcategoryList_SelectedIndexChanged"
Width="180px"
AutoPostBack="True">
</asp:DropDownList>
<Ajax:CascadingDropDown ID="cddSubCategories"
TargetControlID="subcategoryList0"
ServicePath="~/ServiceCS.asmx"
ServiceMethod="GetSubCategories"
runat="server"
Category="CategoryID"
ParentControlID="categoryList0"
LoadingText="Loading..." />
thank you!
Jenise