none
Retain the original searched data when the Back/Refresh button is clicked in Silverlight 2.0?

    Question

  • Hi,

    I have a silverlight 2 application with WCF service. In the main page I have a Search button, which when clicked displayes the searched data in a grid. If I click on a row in the grid the control navigates to another page which has a Back button. When this button is clicked the control returns to the main page, but the grid is empty (page is getting refreshed). Could anyone please tell me how to retain the original searched data on Back button click.

    Regards,

    Carol

    Monday, May 07, 2012 3:25 AM

Answers

  • Hi Carolene ,

        Ok , In the first approach,  How did you store the content of the combobox and grid in App.xaml ?    I suggest you take two IEnumerable collections .

     Usually for populating the datagrid and combobox, you can use their itemsource  property.  It is generally a list or some Ienumerable collection , so it will be stored in App.xaml .   Since you have populated the textbox successfully, this is also similar to that.

    Here are those variables in App.xaml.cs

            public static IEnumerable comboval;
            public static IEnumerable datagridval;
            public static string textboxval;
            public static int comboselectedindex;

    And here are my two pages :   Page 1,( where you have the search button and the combobox, grid and textbox.)

    and the update page ( which basically does nothing but go back to the previous page ).

    Page1.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SearchBack
    {
        public partial class Page1 : UserControl
        {
            public Page1()
            {
                InitializeComponent();

                comboBox1.ItemsSource = "1 2 3 4 5 6 7 8 9".Split(' ');
                dataGrid1.ItemsSource = "a b c d e f g h".Split(' ');
                dataGrid1.AutoGenerateColumns = true;
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                App.textboxval = textBox1.Text;
                App.comboval = comboBox1.ItemsSource;
                App.datagridval = dataGrid1.ItemsSource;
                App.comboselectedindex = comboBox1.SelectedIndex;

                this.Content = new UpdatePage();
            }

            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                if (App.comboval != null && App.datagridval != null && App.textboxval != "")
                {
                    comboBox1.ItemsSource = App.comboval;
                    dataGrid1.ItemsSource = App.datagridval;
                    textBox1.Text = App.textboxval;
                    comboBox1.SelectedIndex = App.comboselectedindex;
                }
            }
        }
    }

    Page1.xaml

    <UserControl x:Class="SearchBack.Page1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Loaded="UserControl_Loaded">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="99,71,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="95,124,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="270,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="99,172,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="174" />
        </Grid>
    </UserControl>
    

    UpdatePage.xaml

    <UserControl x:Class="SearchBack.UpdatePage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <Button Content="Back" Height="23" HorizontalAlignment="Left" Margin="263,40,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,114,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,151,0,0" Name="textBlock2" Text="TextBlock" VerticalAlignment="Top" />
        </Grid>
    </UserControl>
    

    UpdatePage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SearchBack
    {
        public partial class UpdatePage : UserControl
        {
            public UpdatePage()
            {
                InitializeComponent();
            }
       
             
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.Content = new Page1();
            }
        }
    }
    

    Hope it Helps Laughing

    Wednesday, May 09, 2012 2:56 AM

All replies

  • Hi,

      In your App.xaml.cs, add two new variables. 
          public static string SearchString = "";
          public static bool isBack = false;

    They can be used to store your search word , and incase back button is pressed, then that word can be set again.

    In the back button click event, set the App.isBack to true.

    In the MainPage.xaml.cs,  add a loaded event handler, with the following code.

       private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
            {
                if (App.SearchString != "" && App.isBack==true )
                {
                      // write content of App.SearchString
                }
            }

    In case you want the entire grid data, just store it in an object instead of a string in the App.xaml

    Thats all. You should be able to access your searched data.  Hope it Helps Laughing

    Monday, May 07, 2012 6:28 AM
  • Hi Phani,

    Thanks for your reply, but I am sorry, I couldn't get what is being explained by you. I will elaborate my application below so that you will have a better understanding of the same.

    I have a Menupage(Menupage.xaml) in which I have links to my xaml pages. When I run my application, Menu page will be displayed which contains the menu bar with four link buttons(Home,Search,AddNew,About). When I click on Search hyperlink button, Search page(searchpage.xaml) displays within the menupage(in the stackpanel as a child window).
    Search page contains two text boxes, two combo boxes and a datagrid. User enters some value in the text boxes and combo boxes. Upon clicking the search button, the data is fetched from the database and populated in the data grid. Now if user clicks on any row in the datagrid, the control navigates to another page(updatepage.xaml) with all the details, where user can modify the data. The updatepage.xaml page contains back button, when user clicks the back button the control goes back to search page but the text boxes, combo boxes and data grid do not contain any data. Its getting refreshed.
    My requirement is, when user clicks the back button the control should return to the search page with the same state as it was populated with actual search results.
    Can you please elaborate your solution for this issue. Any help will be appriciated.

    Thanks & Regards
    Carol

    Tuesday, May 08, 2012 5:56 AM
  • Hi Carolene,

         In the method i described above,  i was trying to store the data from the Search Page  into a few static variables.  These variables are in the App.xaml page, so they will be available throught the application .  I assumed that when you navigated back to the Search page , you will need to repopulate the comboboxes, textboxes and grid with the previously stored data.

    But after seeing your description, i feel there is another way.  Since you want the search page to be exactly the same ,  why not try adding a new canvas in your child window when the   grid is clicked. This canvas will contain  your Update page as the content where you can update/ view the data.  When you click the back button on the canvas, it will simply remove it from the window.  This way, you wont have to worry about repopulating the search page again , as it wont be changed at all in the fist place.   

    Both the logics will work .  Please let me know if you are able to understand and implement them .

    P.S  :  in the second method , you can set the content to update page as follows : 

    canvas1.Content = new updatePage(//pass parameters for data); 

    Hope it Helps Laughing

    Tuesday, May 08, 2012 6:22 AM
  • Hi ,

    Here is some sample code. My project name is SearchBack.   I have three pages , MainPage, SearchPage  and UpdatePage.  MainPage and Update page are usercontrols, while the SearchPage is a Childwindow. 

    MainPage.Xaml

    <UserControl x:Class="SearchBack.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <Button Content="Open Search page" Height="20" HorizontalAlignment="Left" Margin="32,53,0,0" Name="button1" VerticalAlignment="Top" Width="115" Click="button1_Click" />
        </Grid>
    </UserControl>
    

     

    MainPage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SearchBack
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                SearchPage s = new SearchPage();
                s.Show();
            }
        }
    }
    

    SearchPage.Xaml

    <controls:ChildWindow x:Class="SearchBack.SearchPage"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
               xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
               Width="434" Height="300" 
               Title="SearchPage" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
        <Grid x:Name="LayoutRoot" Margin="2" Width="406">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
    
            <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
            <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="12,35,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="search" Height="23" HorizontalAlignment="Left" Margin="138,35,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <sdk:DataGrid AutoGenerateColumns="False" Height="118" HorizontalAlignment="Left" Margin="12,94,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="223" SelectionChanged="dataGrid1_SelectionChanged" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,56,0,0" Name="textBlock1" Text="(Give space between letters to populate datagrid with dummy data)" VerticalAlignment="Top" />
        </Grid>
    </controls:ChildWindow>

    searchPage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SearchBack
    {
        public partial class SearchPage : ChildWindow
        {
            public SearchPage()
            {
                InitializeComponent();
            }
    
            private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                this.DialogResult = true;
            }
    
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                this.DialogResult = false;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                if (textBox1.Text != "")
                {
                    dataGrid1.AutoGenerateColumns = true;
                    dataGrid1.ItemsSource = textBox1.Text.Split(' ');
                }
            }
    
            Canvas c;
    
            private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                c = new Canvas();
                c.Children.Add(new UpdatePage(LayoutRoot,c));
                if (!LayoutRoot.Children.Contains(c))
                {
                    LayoutRoot.Children.Add(c);
                }
            }
        }
    }
    
    

    UpdatePage.xaml

    <UserControl x:Class="SearchBack.UpdatePage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <Button Content="Back" Height="23" HorizontalAlignment="Left" Margin="263,40,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,114,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,151,0,0" Name="textBlock2" Text="TextBlock" VerticalAlignment="Top" />
        </Grid>
    </UserControl>
    

    UpdatePage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SearchBack
    {
        public partial class UpdatePage : UserControl
        {
            public UpdatePage()
            {
                InitializeComponent();
            }
            Grid gc;
            Canvas cc;
            public UpdatePage(Grid g,Canvas c)
            {
                InitializeComponent();
                gc = g;
                cc = c;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                gc.Children.Remove(cc);
            }
        }
    }
    

     

    This is just sample code based on what you specified. When you click on the dataGrid, you will get that update page, and when you click the back button, it will disappear without modifying your search page.   I tried to simulate the scenario .

    Hope it Helps Laughing

     

    Tuesday, May 08, 2012 7:20 AM
  • Hi Phani,

    Thanks for your quick reply.

    I would like to go with your first approach. But I am not getting where I have to assign the textbox, combobox and grid values to the static strings that has been declared in the App.xaml.cs file. I am new to Silverlight, could you please explain me with an example. I need to finish this ASAP.

    Regards,

    Carol

    Tuesday, May 08, 2012 7:42 AM
  • Hi Phani,

    Thanks a lot for the code sample. I will check it and let you know.

    Regards,

    Carol

    Tuesday, May 08, 2012 7:45 AM
  • Hi Phani,

    I tried the second approach, but the control is displaying the update page instead of search page.

    In my application, all pages are user controls only, I am not using any child window. Menupage is aslo a silverlight user control, inside that I am using a stack panel to display other pages(Searchpage.xaml,Updatepage.xaml.. etc).

    I would like to go with your first approach only and tried to implement that. But I am able to display only the text box values not combo box and datagrid. Kindly can you provide some input on how to set the values to combobox and also how to display the content of the grid with some sample code. 

    Regards

    Carol

    Wednesday, May 09, 2012 1:33 AM
  • Hi Carolene ,

        Ok , In the first approach,  How did you store the content of the combobox and grid in App.xaml ?    I suggest you take two IEnumerable collections .

     Usually for populating the datagrid and combobox, you can use their itemsource  property.  It is generally a list or some Ienumerable collection , so it will be stored in App.xaml .   Since you have populated the textbox successfully, this is also similar to that.

    Here are those variables in App.xaml.cs

            public static IEnumerable comboval;
            public static IEnumerable datagridval;
            public static string textboxval;
            public static int comboselectedindex;

    And here are my two pages :   Page 1,( where you have the search button and the combobox, grid and textbox.)

    and the update page ( which basically does nothing but go back to the previous page ).

    Page1.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SearchBack
    {
        public partial class Page1 : UserControl
        {
            public Page1()
            {
                InitializeComponent();

                comboBox1.ItemsSource = "1 2 3 4 5 6 7 8 9".Split(' ');
                dataGrid1.ItemsSource = "a b c d e f g h".Split(' ');
                dataGrid1.AutoGenerateColumns = true;
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                App.textboxval = textBox1.Text;
                App.comboval = comboBox1.ItemsSource;
                App.datagridval = dataGrid1.ItemsSource;
                App.comboselectedindex = comboBox1.SelectedIndex;

                this.Content = new UpdatePage();
            }

            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                if (App.comboval != null && App.datagridval != null && App.textboxval != "")
                {
                    comboBox1.ItemsSource = App.comboval;
                    dataGrid1.ItemsSource = App.datagridval;
                    textBox1.Text = App.textboxval;
                    comboBox1.SelectedIndex = App.comboselectedindex;
                }
            }
        }
    }

    Page1.xaml

    <UserControl x:Class="SearchBack.Page1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Loaded="UserControl_Loaded">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="99,71,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="95,124,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="270,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="99,172,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="174" />
        </Grid>
    </UserControl>
    

    UpdatePage.xaml

    <UserControl x:Class="SearchBack.UpdatePage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <Button Content="Back" Height="23" HorizontalAlignment="Left" Margin="263,40,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,114,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="80,151,0,0" Name="textBlock2" Text="TextBlock" VerticalAlignment="Top" />
        </Grid>
    </UserControl>
    

    UpdatePage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SearchBack
    {
        public partial class UpdatePage : UserControl
        {
            public UpdatePage()
            {
                InitializeComponent();
            }
       
             
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.Content = new Page1();
            }
        }
    }
    

    Hope it Helps Laughing

    Wednesday, May 09, 2012 2:56 AM
  • Hi Phani,

    Thanks for your reply.

    I will try this and let you know.

    Regards

    Carol

    Wednesday, May 09, 2012 5:50 AM
  • Hi Phani,

    Thank you very much. Now the application is working fine as per the requirements.

    Thanks a lot...

    Regards

    Carol

    Wednesday, May 09, 2012 7:21 AM
  • Hi Phani,

    I am able to view the content of the search page now. But the columns of the datagrid are not in the proper order. I tried to reorder the columns but still its coming in the same manner.

    In search page, I was able to reorder the columns but not in the back button event.

    Can you provide some inputs on this. Any help would be appreciated.

    Regards,

    Carol

    Friday, May 11, 2012 2:30 AM