Click here to Skip to main content
15,887,450 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I have the following code of cascading dropdownlist using web service. What i want to do is to bind both the Text and value field from the database in each of the dropdownlist. I am not getting the selected value from the dropdownlist when i tried to do so.

Web Service

C#
[WebMethod]
    public CascadingDropDownNameValue[] GetGrandParentServices(string knownCategoryValues)
    {
        string query = "SELECT ServiceType FROM ServiceType Where ParentServiceID IS NULL";
        List<CascadingDropDownNameValue> serviceType = GetData(query);
        return serviceType.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetParentServices(string knownCategoryValues)
    {
        string parentService = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["ServiceType"];
        string query = string.Format("SELECT DISTINCT ParentService FROM Service WHERE GrandParentService = '{0}'", parentService);
        List<CascadingDropDownNameValue> services = GetData(query);
        return services.ToArray();
    }


private List<CascadingDropDownNameValue> GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].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[0].ToString()
                    });
                }
                reader.Close();
                con.Close();
                return values;
            }
        }
    }



My .aspx Page


<pre lang="HTML"><EditItemTemplate>
           <asp:DropDownList ID="ddlGrandParentService" runat="server" Width="150">
           </asp:DropDownList>
           <cc1:CascadingDropDown ID="cdlGrandParentService" TargetControlID="ddlGrandParentService" PromptText="Select Parent Service" PromptValue=""
                                ServicePath="http://localhost:1232/ServiceCS.asmx" ServiceMethod="GetGrandParentServices"
                                 runat="server" Category="ServiceType" />
            </EditItemTemplate>


      <EditItemTemplate>
           <asp:DropDownList ID="ddlParentService" runat="server" Width="150">
                            </asp:DropDownList>

           <cc1:CascadingDropDown ID="cdlParentService" TargetControlID="ddlParentService" PromptText="Select Parent Service" PromptValue=""
                                ServicePath="http://localhost:1232/ServiceCS.asmx" ServiceMethod="GetParentServices"  runat="server"
                                Category="ParentService" ParentControlID="ddlGrandParentService" />

          </EditItemTemplate>





Code Behind

C#
int GrandParentServiceId = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlGrandParentService")).SelectedItem.Value);
 int ParentServiceId = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlParentService")).SelectedItem.Value);



Please help me how do i achieve this problem. Thanking you in advance.
Posted
Comments
bowlturner 27-Dec-13 17:10pm    
What's happening? any errors? or just nothing?
apurba001 27-Dec-13 17:51pm    
I unable to get the value of the dropdownlist.
ZurdoDev 27-Dec-13 20:47pm    
1. Reply to the comment so that the user is notified.
2. You need to be more clear. What do you get?
Inside which event you are trying to get the value and what values you are getting now?
apurba001 28-Dec-13 14:59pm    
I get nothing inside the code behind method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900