populating combobox asynchronasly

can any one tell me how u can populate 2nd combobox using first combobox selection in Silverlight3 but i need it asynchronasly like ajax.please give me code example
Thanks
farjack1Asked:
Who is Participating?
 
nmarunCommented:
In the code below, I have a list of states and once the user selects a state, all the cities of the state appear in the second combo box.

// xaml code:
<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions >
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
   
    <ComboBox Name="StateComboBox" Grid.Row="0" Grid.Column="0" Height="35"
              SelectionChanged="StateComboBox_SelectionChanged" Width="75" />
    <ComboBox Name="CityComboBox" Grid.Row="0" Grid.Column="1" Height="35"
              Width="150" />
    <TextBlock Name="MessageBlock" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
               Text="Loading Cities..." FontSize="15" />
</Grid>

// code-behind
public partial class MainPage : UserControl
{
    // add Service Reference:
    // http://www.webservicex.net/uszip.asmx
    // to the SL project
    public MainPage()
    {
        InitializeComponent();
        Loaded += MainPage_Loaded;
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        MessageBlock.Visibility = Visibility.Collapsed;
        StateComboBox.Items.Add("AZ");
        StateComboBox.Items.Add("CA");
        StateComboBox.Items.Add("FL");
        StateComboBox.Items.Add("HI");
    }

    private void StateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBlock.Visibility = Visibility.Visible;
        CityComboBox.Items.Clear();
        USZipSoapClient zipSoapClient = new USZipSoapClient();
        zipSoapClient.GetInfoByStateCompleted += ZipSoapClient_GetInfoByStateCompleted;
        string stateCode = StateComboBox.SelectedItem.ToString();
        zipSoapClient.GetInfoByStateAsync(stateCode);
    }

    void ZipSoapClient_GetInfoByStateCompleted(object sender, GetInfoByStateCompletedEventArgs e)
    {
        string xmlResult = e.Result.ToString();
        XmlReader resultXml = XmlReader.Create(new StringReader(xmlResult));
        while (resultXml.Read())
        {
            if (resultXml.NodeType != XmlNodeType.Element) continue;
            switch (resultXml.LocalName.ToLower())
            {
                case "city":
                    string cityName = resultXml.ReadInnerXml();
                    if(!CityComboBox.Items.Contains(cityName))
                    {
                        CityComboBox.Items.Add(cityName);
                    }
                    break;
            }
        }
        MessageBlock.Visibility = Visibility.Collapsed;
    }
}
0
 
farjack1Author Commented:
if i do not use web service and getting data from database still silverlight will populate cityCombobox asynchronously?
0
 
nmarunCommented:
Yes, that is true. A good number of things in Silverlight operate asynchronously.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.