This cant be THAT hard:
I have 3 cascading dropdownlists, A, B and C.
I have saved the data correctly to my DB, value for A=4, value for B=3 and value for C=3
Now when I load my page, I want to set these values,
So list A must show all items and option 4 is selected.
list B must show all items and option 3 is selected.
list C must show all items and option 3 is selected.
However, since the dropdownlists are cascaded lists B and C do not contain any items yet as no choice has been made in A.
Here are two dropdowns to begin with:
<asp:DropDownList cssclass="textbox" ID="ddlCatlevel1E" OnDataBound="ddlCatlevel1E_DataBound" runat="server"/>
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="ddlCatlevel1E" Category="Catlevel1"
prompttext="Select maincategory" ServicePath="/categories.asmx" ServiceMethod="GetCatlevel1">
</cc1:CascadingDropDown>
<asp:DropDownList cssclass="textbox" ID="ddlCatlevel2" runat="server"/>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server"
TargetControlID="ddlCatlevel2" ParentControlID="ddlCatlevel1E"
Category="Catlevel2" prompttext="Select subcategory" UseContextKey="true" ContextKey="ddlCatlevel1E"
ServicePath="/categories.asmx" ServiceMethod="GetCatlevel2"
LoadingText="loading...">
</cc1:CascadingDropDown>
categories.vb
<WebMethod()> _
Public Function GetCatlevel1(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim catlevel1Adapter As categoryTableAdapters.Catlevel1TableAdapter = New categoryTableAdapters.Catlevel1TableAdapter
Dim cat1levels As category.Catlevel1DataTable = catlevel1Adapter.GetDataByCatlevel1
Dim values As New List(Of CascadingDropDownNameValue)
For Each dr As DataRow In cat1levels
Dim title As String = CType(dr("title"), String)
Dim id As Integer = CType(dr("id"), Integer)
values.Add(New CascadingDropDownNameValue(title, id.ToString))
Next
Return values.ToArray
End Function
<WebMethod()> _
Public Function GetCatlevel2(ByVal knownCategoryValues As String, ByVal category As String, ByVal contextKey As String) As CascadingDropDownNameValue()
Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
Dim UpperCatlevelID As Integer '= CInt(category)
If Not kv.ContainsKey("Catlevel1") OrElse Not Int32.TryParse(kv("Catlevel1"), UpperCatlevelID) Then
Return Nothing
End If
Dim MyConnection As SqlConnection = GetConnection()
Dim cmd As New SqlCommand("SELECT id,title FROM catlevel2 WHERE catlevel1=@catlevel1id ORDER BY title ASC", MyConnection)
cmd.Parameters.Add(New SqlParameter("@catlevel1id", UpperCatlevelID))
Dim values As New List(Of CascadingDropDownNameValue)
Try
MyConnection.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
values.Add(New CascadingDropDownNameValue(reader("title").ToString, reader("id").ToString))
End While
Catch ex As Exception
Finally
MyConnection.Close()
End Try
Return values.ToArray
End Function
(I know that the first dropdown doesnt necessarily have to be cascading, but for this example its necessary)
Now, how do I set the selectedvalue of the 2nd dropdown based on the default value in the first dropdown? I think I have to do something with the contextkey.
The dropdownlists reside in a detailsview, so I also need to know in which event I need to execute the code for settings the selected dropdownlists values.
Please a code example!
↧
mutltiple cascading dropdownlists default values
↧