Select to view content in your preferred language

Query with radio button or check box

1351
8
Jump to solution
06-04-2012 01:52 PM
TanyaOwens
Frequent Contributor
Hello,
I would like to set up a query that is created from the user checking a check box or clicking a radio button. For example, the user would check a box next to wheelchair accessible and the results would return services that are wheelchair accessible into a datagrid. Most of the codes I see are where the user inputs text to create the query. Does anyone know of an example code for query by check box or radio button?

Thanks!
0 Kudos
1 Solution

Accepted Solutions
JoeHershman
MVP Alum
Assuming you are always having the button content be the name of the state you can avoid a long if block with;

        private void RadioButton_Click(object sender, RoutedEventArgs e)         {             // Query task initialization.             QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +               "Demographics/ESRI_Census_USA/MapServer/5");             queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;             queryTask.Failed += QueryTask_Failed;               // Query task parameters. Return geometry, state, and population density.             Query query = new Query();             query.ReturnGeometry = true;             query.OutFields.AddRange(new string[] { "STATE_NAME"});                          //The Clicked Radio button is always the one selected..             RadioButton button = sender as RadioButton;             if ( button == null ) return;               query.Where = "STATE_NAME = '" + button.Content + "'";               queryTask.ExecuteAsync(query);         }
Thanks,
-Joe

View solution in original post

0 Kudos
8 Replies
DominiqueBroux
Esri Frequent Contributor
I have no ready to use sample but try to add a click event handler to your radio buttons, and, in that event handlers, change the where clause of your feature layer and update the feature layer.
0 Kudos
TanyaOwens
Frequent Contributor
Using the query concept code http://help.arcgis.com/en/webapi/silverlight/help/index.html#/Query_task/01660000001t000000/, I am a little lost about what to do with the code at the "query.Where = QueryTextBox.Text;"

I have added the following xaml:
        <!-- QUERY TASK INTERFACE -->
        <StackPanel HorizontalAlignment="Left" Margin="10">
            <Grid>
                <Rectangle Fill="Orange" 
                           Stroke="Gray"  
                           RadiusX="10" 
                           RadiusY="10" />
                
                <StackPanel Orientation="Vertical" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3">
                    <TextBlock x:Name="QueryData" 
                                   Text="Do you have any special needs for your trip?" 
                                   Foreground="White" 
                                   FontSize="20" FontWeight ="ExtraBold"
                                   Grid.Row="0"                      
                                   Margin="15,5,15,1" 
                                   HorizontalAlignment="Center" >
                    </TextBlock>

                    <RadioButton 
                        x:Name="AZ"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White"  
                        Content="Arizona">
                    </RadioButton>

                    <RadioButton
                        x:Name="NM"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White" 
                        Content="New Mexico" >
                    </RadioButton>

                    <RadioButton
                        x:Name="NY"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White" 
                        Content="New York" >
                    </RadioButton>
                </StackPanel>
            </Grid>
        </StackPanel>


And this is what I have so far for the c# code:

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            // Query task initialization.
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
              "Demographics/ESRI_Census_USA/MapServer/5");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            // Query task parameters. Return geometry, state, and population density.
            Query query = new Query();
            query.ReturnGeometry = true;
            query.OutFields.AddRange(new string[] { "STATE_NAME"});

            // Use textbox text as query condition. 
            //query.Where = QueryTextBox.Text; 
            query.Where = 
                if (AZ.IsChecked == true)

            else if (NM.IsChecked == true)

            else if (NY.IsChecked == true)

            queryTask.ExecuteAsync(query);
        }
0 Kudos
LanceCrumbliss
Frequent Contributor
Using the query concept code http://help.arcgis.com/en/webapi/silverlight/help/index.html#/Query_task/01660000001t000000/, I am a little lost about what to do with the code at the "query.Where = QueryTextBox.Text;"

I have added the following xaml:
        <!-- QUERY TASK INTERFACE -->
        <StackPanel HorizontalAlignment="Left" Margin="10">
            <Grid>
                <Rectangle Fill="Orange" 
                           Stroke="Gray"  
                           RadiusX="10" 
                           RadiusY="10" />
                
                <StackPanel Orientation="Vertical" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3">
                    <TextBlock x:Name="QueryData" 
                                   Text="Do you have any special needs for your trip?" 
                                   Foreground="White" 
                                   FontSize="20" FontWeight ="ExtraBold"
                                   Grid.Row="0"                      
                                   Margin="15,5,15,1" 
                                   HorizontalAlignment="Center" >
                    </TextBlock>

                    <RadioButton 
                        x:Name="AZ"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White"  
                        Content="Arizona">
                    </RadioButton>

                    <RadioButton
                        x:Name="NM"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White" 
                        Content="New Mexico" >
                    </RadioButton>

                    <RadioButton
                        x:Name="NY"
                        Click="RadioButton_Click"
                        GroupName="STATE_NAME"                             
                        Foreground="White" 
                        Content="New York" >
                    </RadioButton>
                </StackPanel>
            </Grid>
        </StackPanel>


And this is what I have so far for the c# code:

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            // Query task initialization.
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
              "Demographics/ESRI_Census_USA/MapServer/5");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            // Query task parameters. Return geometry, state, and population density.
            Query query = new Query();
            query.ReturnGeometry = true;
            query.OutFields.AddRange(new string[] { "STATE_NAME"});

            // Use textbox text as query condition. 
            //query.Where = QueryTextBox.Text; 
            query.Where = 
                if (AZ.IsChecked == true)

            else if (NM.IsChecked == true)

            else if (NY.IsChecked == true)

            queryTask.ExecuteAsync(query);
        }


Try

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            // Query task initialization.
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
              "Demographics/ESRI_Census_USA/MapServer/5");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            // Query task parameters. Return geometry, state, and population density.
            Query query = new Query();
            query.ReturnGeometry = true;
            query.OutFields.AddRange(new string[] { "STATE_NAME"});

            // Use textbox text as query condition. 
            //query.Where = QueryTextBox.Text; 
                if (AZ.IsChecked == true)
                   query.Where = "STATE_NAME = 'Arizona'";
            else if (NM.IsChecked == true)
                   query.Where = "STATE_NAME = 'New Mexico'";
            else if (NY.IsChecked == true)
                   query.Where = "STATE_NAME = 'New York'";
            queryTask.ExecuteAsync(query);
        }
0 Kudos
JoeHershman
MVP Alum
Assuming you are always having the button content be the name of the state you can avoid a long if block with;

        private void RadioButton_Click(object sender, RoutedEventArgs e)         {             // Query task initialization.             QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +               "Demographics/ESRI_Census_USA/MapServer/5");             queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;             queryTask.Failed += QueryTask_Failed;               // Query task parameters. Return geometry, state, and population density.             Query query = new Query();             query.ReturnGeometry = true;             query.OutFields.AddRange(new string[] { "STATE_NAME"});                          //The Clicked Radio button is always the one selected..             RadioButton button = sender as RadioButton;             if ( button == null ) return;               query.Where = "STATE_NAME = '" + button.Content + "'";               queryTask.ExecuteAsync(query);         }
Thanks,
-Joe
0 Kudos
TanyaOwens
Frequent Contributor
Thanks for the help everyone. The code that Lance provided works for me but I was really interested in the code provided by Joe. This would be nice but unfortunately I can't seem to get it to work. It doesn't return any errors - it just doesn't do anything with I click the buttons during debugging. Any thoughts?
0 Kudos
JoeHershman
MVP Alum
Thanks for the help everyone. The code that Lance provided works for me but I was really interested in the code provided by Joe. This would be nice but unfortunately I can't seem to get it to work. It doesn't return any errors - it just doesn't do anything with I click the buttons during debugging. Any thoughts?


Not clear on doesn't do anything. 

I assume it is going into the method and gets to the line below

    if ( button == null ) return;


Check what the Query:Where is with a Debug statement or in the watch window to confirm is same as before.

I run the same code with the buttons copied out of your post and it all worked

Good luck
-Joe
Thanks,
-Joe
0 Kudos
TanyaOwens
Frequent Contributor
Thanks Joe. I finally got it to work. I must have been having a bad day and I had accidentally commented the "queryTask.ExecuteAsync(query);". Once I went back a looked at it today, I saw the error and it works perfectly.
0 Kudos
TanyaOwens
Frequent Contributor
Follow up question: I would like the radiobutton click to query one or more at a time. For example, I would like to query if a service has Walker and Oxygen Tank accommodations or just query "Service Animal" only or "Walker" only. I figured out I could allow for multiple radio buttons to be clicked by having individual GroupName but I am not sure what to do with the code behind. Currently my code looks like this:
            if (OxygenTank.IsChecked == true)
                query.Where = "OxygenTank = 'Oxygen Tank'";

            else if (ServiceAnimal.IsChecked == true)
                query.Where = "ServiceAnimal = 'Service Animal'";

            else if (Walker.IsChecked == true)
                query.Where = "Walker = 'Walker'";


Any suggestions would be greatly appreciated.
0 Kudos