/// <summary>
        /// Saves the GridViewSearchPanel State.
        /// </summary>
        /// <param name="controlState">State of the control.</param>
        /// <param name="controlStateName">Name of the control state.</param>
        /// <param name="persistenceMethod">The persistent method.</param>
        /// <param name="persistentCookieExpiryDateTime">The persistent cookie expiry datetime.</param>
        public static void SaveGridViewSearchPanelState( GridViewSearchPanelState controlState, string controlStateName, 
															PersistenceMethod persistenceMethod, DateTime? persistentCookieExpiryDateTime )
        {
			switch ( persistenceMethod )
			{
				case PersistenceMethod.Session :
					#region Uses Session state
					HttpContext.Current.Session[ controlStateName ] = controlState;
					#endregion
					break;
				case PersistenceMethod.Cookie :
				case PersistenceMethod.PersistentCookie :
					#region Uses a client cookie
		            string serializedString = SerializeGridViewSearchPanelState( controlState );
			        HttpCookie cookie = new HttpCookie( controlStateName, HttpContext.Current.Server.UrlEncode( serializedString ) );
			        // Determine if this should be persisted longer than just the current session
			        if ( persistentCookieExpiryDateTime != null )
			        {
						cookie.Expires = persistentCookieExpiryDateTime.Value;
			        }
					HttpContext.Current.Response.Cookies.Add( cookie );

					#endregion
					break;
			}
        }
		/// <summary>
        /// Serializes the grid view search panel state.
        /// </summary>
        /// <param name="controlState">The control state.</param>
        /// <returns></returns>
        private static string SerializeGridViewSearchPanelState( GridViewSearchPanelState controlState)
        {
            string serializedString = string.Empty;

            using ( StringWriter stringWriter = new StringWriter() )
            {
                XmlSerializer serializer = new XmlSerializer( typeof( GridViewSearchPanelState ) );
                serializer.Serialize( stringWriter, controlState );
                serializedString = stringWriter.ToString();
            }

            return serializedString;
        }
        /// <summary>
        /// Handles the state of the GridViewSearchPanel
        /// </summary>
        /// <param name="searchPanel">The search panel.</param>
        /// <param name="gridView">The grid view.</param>
        /// <param name="persistenceMethod">The persistence method</param>
        /// <param name="persistentCookieExpiryDateTime">The date and time the persistent cookie expires</param>
        public static void HandleGridViewSearchPanelState( GridViewSearchPanel searchPanel, GridView gridView, 
															PersistenceMethod persistenceMethod, DateTime? persistentCookieExpiryDateTime )
        {
			if ( gridView == null )
			{
				// try and find the GridView control if it hasn't been explicitly supplied
	            gridView = FindControl( searchPanel.NamingContainer, searchPanel.GridViewControlID ) as GridView;
			}
			
            if ( gridView != null )
            {
                // restore any previously saved state
                gridView.Page.Load += new EventHandler(
                    delegate( object sender, EventArgs e )
                    {
                        if ( !gridView.Page.IsPostBack )
                        {
                            GridViewSearchPanelState controlState = GetGridViewSearchPanelState( GetUniqueGridViewName( gridView ), persistenceMethod );
                            if ( controlState != null )
                            {
                                controlState.RestoreState( ref gridView, ref searchPanel );                                
                            }
                        }
                    }
                );

                // save state on row command, and rebind searchPanel
                gridView.RowCommand += new GridViewCommandEventHandler(
                    delegate(object sender, GridViewCommandEventArgs e)
                    {
                        GridViewSearchPanelState controlState = new GridViewSearchPanelState(gridView, searchPanel);
                        SaveGridViewSearchPanelState(controlState, GetUniqueGridViewName(gridView),
                                                        persistenceMethod, persistentCookieExpiryDateTime);
                        searchPanel.DataBind();
                    }    
                );

                // save state when page index changes
                gridView.PageIndexChanged += new EventHandler(
                    delegate( object sender, EventArgs e )
                    {
                        GridViewSearchPanelState controlState = new GridViewSearchPanelState( gridView, searchPanel );
                        SaveGridViewSearchPanelState( controlState, GetUniqueGridViewName( gridView ), 
														persistenceMethod, persistentCookieExpiryDateTime );
                    }
                );

                // save state when grid is sorted
                gridView.Sorted += new EventHandler(
                    delegate( object sender, EventArgs e )
                    {
                        GridViewSearchPanelState controlState = new GridViewSearchPanelState( gridView, searchPanel );
                        SaveGridViewSearchPanelState( controlState, GetUniqueGridViewName( gridView ), 
														persistenceMethod, persistentCookieExpiryDateTime );
                        
                    }
                );

                if (gridView is EntityGridView)
                {
                    // save state when grid's page size changed
                    ((EntityGridView)gridView).PageSizeChanged += new EventHandler(
                        delegate(object sender, EventArgs e)
                        {
                            GridViewSearchPanelState controlState = new GridViewSearchPanelState(gridView, searchPanel);
                            SaveGridViewSearchPanelState(controlState, GetUniqueGridViewName(gridView),
                                                            persistenceMethod, persistentCookieExpiryDateTime);
                        }
                    );
                }
                // save state when search id done
                searchPanel.SearchButtonClicked += new EventHandler(
                    delegate( object sender, EventArgs e )
                    {
                        GridViewSearchPanelState controlState = new GridViewSearchPanelState( gridView, searchPanel );
                        SaveGridViewSearchPanelState( controlState, GetUniqueGridViewName( gridView ), 
														persistenceMethod, persistentCookieExpiryDateTime );
                    }
                );

                // save state when reset is done
                searchPanel.ResetButtonClicked += new EventHandler(
                    delegate( object sender, EventArgs e )
                    {
                        GridViewSearchPanelState controlState = new GridViewSearchPanelState( gridView, searchPanel );
                        SaveGridViewSearchPanelState(controlState, GetUniqueGridViewName(gridView),
                                persistenceMethod, persistentCookieExpiryDateTime);
                    }
                );
            }
        }