drinksbreak 0 Newbie Poster

Hi Everyone

Ive created 2 cascading drop down lists that gets populated using the cascading drop down tool from the asp.net AJAX control toolkit,

Their use is that the user will select what country they are from and then the approriate states will be displayed in the following drop down.

This code comes from my ascx page, as I use custom user controls , which then gets loaded into my aspx page.

The drop down's get populated with the correct data, a webservice is used for this, and that works fine but when I need to assign the selectedItems.Value or SelectedItems.Text it does not assign anything.

Ive created a label to check if it populates for testing purposes, the data will actual get sent to the SQL database.

The AJAX control uses a Webservice to populate the data.

Thanks for any help


Here is some of my ascx page

<tr>
                    
            <td>Country :</td><td>
                <asp:DropDownList ID="dd_country" runat="server" >
                </asp:DropDownList>
                    
                </td><td>
                </td></tr>
                <tr>
            <td>State :</td><td>
                <asp:DropDownList ID="dd_states" runat="server" OnSelectedIndexChanged="dd_states_SelectedIndexChanged" AutoPostBack="True">
                </asp:DropDownList>
                </td><td>
                </td></tr>
                
                <cc1:CascadingDropDown 
                ID="CascadingDropDown1" 
                runat="server"
                TargetControlID="dd_country"
                Category="Countries"
                PromptText="Select a Country"
                ServicePath="~/user_controls/services/Countries.asmx"
                ServiceMethod="GetCountries" />
                
                <cc1:CascadingDropDown 
                ID="CascadingDropDown2" 
                runat="server"
                TargetControlID="dd_states"
                ParentControlID="dd_country"
                PromptText="Please select a State"
                ServiceMethod="GetStatesForCountries"
                ServicePath="~/user_controls/services/Countries.asmx"
                Category="States" />


****

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:Label ID="Label11" runat="server"></asp:Label>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="dd_states" EventName="SelectedIndexChanged" />
                        </Triggers>
                </asp:UpdatePanel>

Here is the code in the ascx.cs page

public void dd_states_SelectedIndexChanged(object sender, EventArgs e)
    {
        string country = dd_country.SelectedItem.Text;
        string states = dd_states.SelectedItem.Text;
        // These values are always coming up empty
        Label11.Text = string.Format("You selected <b>{0}</b> from <b>{1}</b> country.", states, country);

    }

I have set the aspx page property EnableEventValidation="false"

here is the webservice page if you want to look at it - but all works fine with getting and populating the drop down data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Collections.Specialized;
using System.Web.Services.Protocols;
using AjaxControlToolkit;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; // Use this with retrieving the connection string



/// <summary>
/// Summary description for Coutries / States
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Countries : System.Web.Services.WebService {

    public Countries () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category) 
        { 
                    
            string connStr = ConfigurationManager.ConnectionStrings["BB1_x2sConnectionString"].ConnectionString;
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from countries ORDER BY country", connStr);

            //Create and fill the DataSet.
            DataSet ds2 = new DataSet();
            cmd1.Fill(ds2, "countries");
            DataTable dt1 = ds2.Tables["countries"];
        
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); 
            foreach (DataRow dr in dt1.Rows) 
                { 
                    string cntry = (string)dr["country"]; 
                    int country_id = (int)dr["id"];
                    values.Add(new CascadingDropDownNameValue(cntry, country_id.ToString())); 
                } 
            return values.ToArray(); 
        }
    
    [WebMethod]
    public CascadingDropDownNameValue[] GetStatesForCountries(string knownCategoryValues, string category) 
        { 
            StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
                   
            int country_id;
            if (!kv.ContainsKey("Countries") || !Int32.TryParse(kv["Countries"], out country_id)) 
            { 
                return null; 
            } 
                       
            string connStr = ConfigurationManager.ConnectionStrings["BB1_x2sConnectionString"].ConnectionString;
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from states WHERE country_id = '" + country_id + "' ORDER BY state", connStr);
            
            //Create and fill the DataSet.
            DataSet ds3 = new DataSet();
            cmd1.Fill(ds3, "states");
            DataTable dt2 = ds3.Tables["states"];
                   
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); 
            foreach (DataRow dr in dt2.Rows) 
            { 
                values.Add(new CascadingDropDownNameValue((string)dr["state"], dr["id"].ToString())); 
            } 
            
            return values.ToArray(); 
        }


        
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.