?
Solved

populating combobox asynchronasly

Posted on 2010-01-02
3
Medium Priority
?
479 Views
Last Modified: 2013-11-12
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
0
Comment
Question by:farjack1
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 26165022
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
 

Author Comment

by:farjack1
ID: 26165711
if i do not use web service and getting data from database still silverlight will populate cityCombobox asynchronously?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 26167469
Yes, that is true. A good number of things in Silverlight operate asynchronously.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question