How can return to my previous set of results?

Last post 11-12-2009 8:39 AM by rtpHarry. 8 replies.

Sort Posts:

  • How can return to my previous set of results?

    11-12-2009, 5:09 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 4:32 PM
    • Posts 405

    Hi, 

    I am using the following piece of code to take me back to my previous page.

    This page I want to redirect to is a set of search results.

    I have the following bit of code, this does work but when I click the close button a page comes up which says “Webpage has expired”. If I then refresh the page it does go back to the previous set of results.

    Does anyone know how I can over come this step of having to manually refresh the page after the button click to display the previous set of search results?

    This is my button click code

    btnClose.Attributes.Add("onclick", "javascript:history.back(); return false;");

    Thank you

  • Re: How can return to my previous set of results?

    11-12-2009, 5:48 AM
    • All-Star
      36,899 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,953

    I think this happens when the page before hand has been posted back (ie IsPostBack would = true).

    This is because web browsers don't know about asp.net and they dont know that we use the <form> tag for special purposes. Because of this it tries to prevent submitting the form data a second time because it could be a purchase or something that you dont want resubmitting.

    Long story short if you want to get around this then you should modify your search results to take the search terms via the querystring. That way you can do a response.redirect from the search box to the search results page and you will be able to navigate back to it without encountering a postback'd page.


  • Re: How can return to my previous set of results?

    11-12-2009, 6:00 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 4:32 PM
    • Posts 405

    thansk for your reply. i dont really understand  what your solution is. can you please explain it again or with an example of what to do

    this is soemthing i really need to implement and dont have a clure.

    thanks for your help

  • Re: How can return to my previous set of results?

    11-12-2009, 6:05 AM
    • All-Star
      36,899 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,953

    young345:

    thansk for your reply. i dont really understand  what your solution is. can you please explain it again or with an example of what to do

    this is soemthing i really need to implement and dont have a clure.

    thanks for your help

    I can try to help you out but an example is a bit tricky because it depends on how you have setup all the bits of your project.

    What is your current scenario when a search occurs? Is the search performed on the page or does it post from another page? Do you have some kind of gridview bound to a control property such as a textbox and a search button to refresh the page?


    I tell you what, if you post up the markup and the code behind for your search page that would be a good starting point.

  • Re: How can return to my previous set of results?

    11-12-2009, 6:24 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 4:32 PM
    • Posts 405
    Thanks,

     

    I have a page where a user enters a date and clicks a go button which displays the results in a grid, then the user clicks on a link in this grid which takes them to another page.

    From this other page I have a close button which when clicked should redirect the user back to the previous search results.

    This is the search page mark-up

    <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    
        <script type="text/javascript">
            function popupCalendar(txtDateBox) 
            {
                var SearchDate = document.getElementById('SearchDate');
                txtDateBox.value = "";
                if (SearchDate.style.display == 'none')
                    SearchDate.style.display = 'block';
                else
                    SearchDate.style.display = 'none';
            }
    
        </script>
    
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblDays" runat="server" Text="Search" 
                        CssClass="Text" />
                </td>
                <td>
                    <asp:TextBox ID="txtDateBox" runat="server" onclick="popupCalendar(this);" 
                        ReadOnly="True" EnableViewState="false" CssClass="Text"/>
                        <asp:HiddenField id="dateval" runat="server" />
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ErrorMessage="Please select a date" ControlToValidate="txtDateBox" 
                    Display="Dynamic" CssClass="Validation"/>
                </td>
                <td>
                    <asp:Button ID="btnSearch2" runat="server" Text="Go" CssClass="Text"  />
                </td>
                
            </tr>     
            <tr>
                <td colspan="1">
                    <asp:Label ID="lblDateResults" runat="server" CssClass="Text" />
                </td>
            </tr> 
           
        </table>   
          
          <div id="SearchDate" style="display:none; position:absolute; z-index:2;" class="SearchDate">
              <asp:Calendar id="Calendar1" OnSelectionChanged="calDate_SelectionChanged" 
                  Runat="server" BorderStyle="Solid" BorderWidth="1px" BorderColor="Black"  WeekendDayStyle-Wrap="False" 
                  DayHeaderStyle-ForeColor="Blue" DayHeaderStyle-VerticalAlign="Middle" 
                  DayHeaderStyle-HorizontalAlign="Center" 
                  OtherMonthDayStyle-CssClass="SearchDate" Font-Size="Small" 
                  TitleStyle-BackColor="#009B00" ForeColor="White" 
                  ondayrender="Calendar1_DayRender" BackColor="White">
                  <TodayDayStyle ForeColor="#FF6666" />
                  <WeekendDayStyle Wrap="False" />
                  <OtherMonthDayStyle ForeColor="#CCCCCC" />
                  <DayStyle Font-Underline="False" ForeColor="Black" />
                  <DayHeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="#5D7B9D"></DayHeaderStyle>
                  <TitleStyle BackColor="#5D7B9D" />
              </asp:Calendar>
          </div>  
        
        <br />
        <br />
        <div id="GridDiv" style="position:absolute; z-index:1;">
        <asp:GridView ID="gridSearchResults" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" AutoGenerateColumns="False" CssClass="Grid" >
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                
                <asp:HyperLinkField Text="View/Edit" DataNavigateUrlFields="Id" 
                    DataNavigateUrlFormatString="Order.aspx?id={0}" />
                
                <asp:BoundField DataField="Name" HeaderText="Company Name" ReadOnly="True" 
                    SortExpression="Name" />      
                
                <asp:BoundField DataField="DateRequired" HeaderText="Date Required" 
                    ReadOnly="True" SortExpression="DateRequired" 
                    DataFormatString="{0:dd/MM/yyyy}" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <EmptyDataTemplate>
                Your search returned no results.
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        </div>
    
        <script type="text/javascript">
         var txtDateBox = document.getElementById('<%= txtDateBox.ClientID%>');
         var txtDateBox = document.getElementById('<%= txtDateBox.ClientID%>');
         if (txtDateBox.value == "" && <%=IsPostBack == true ? "1" : "0" %> == "1")
         {
            var SearchDate = document.getElementById('SearchDate');
            SearchDate.style.display = 'block';
            
        }
         if (<%=gridJobSearchResults.Visible == true ? "1" : "0" %> == "1")
         {
            var SearchDate = document.getElementById('SearchDate');
            var txtDateBox = document.getElementById('<%= txtDateBox.ClientID%>');
            var dateval = document.getElementById('<%= dateval.ClientID%>');
            txtDateBox.value = dateval.value;
            SearchDate.style.display = 'none';
         }
        
        </script>
        
        </asp:Content>
    

    This is the code behind page of the page that the user is taken to when they click on the link in the grid of the search results.

    In the page load I wrote this - where I mentioned before I had the problem

    protected void Page_Load(object sender, EventArgs e)
            {
    		btnClose.Attributes.Add("onclick", "javascript:history.back(); return false;");
            }
    

    Is this any help. Can you see what needs to be done?

  • Re: How can return to my previous set of results?

    11-12-2009, 6:55 AM
    • All-Star
      36,899 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,953

    What piece of code ties the value entered into the search date textbox to the filter being applied to the gridview?

    do you have more code in the codebehind?

    looking at it the gridview doesn't have a datasource configured and the search button doesnt have a click event handler so I am at a loss at the moment as to how this works?

  • Re: How can return to my previous set of results?

    11-12-2009, 7:35 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 4:32 PM
    • Posts 405

    thanks but i'll try and figure it out, thats my code. the other code behind stuff is none related to this query so there is no point.

    also this code behind is not the page of the search results

    if anyone knows of any examples how to do this i would really appreciate it 

  • Re: How can return to my previous set of results?

    11-12-2009, 8:30 AM
    Answer
    • Contributor
      5,228 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 873

    If you enable client-side caching of the page, that should allow the browser to go back to it without forcing a re-post.

    Try adding something like this after your Page directive in your .aspx file:

    <%@ OutputCache Duration="300" Location="Client" VaryByParam="None" %>
    



  • Re: How can return to my previous set of results?

    11-12-2009, 8:39 AM
    Answer
    • All-Star
      36,899 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,953

    young345:

    thanks but i'll try and figure it out, thats my code. the other code behind stuff is none related to this query so there is no point.

    also this code behind is not the page of the search results

    if anyone knows of any examples how to do this i would really appreciate it 

    Ok well without understanding how your page gets its results together I am limited in how I can help you.


    The theory goes like this:

    Setup a gridview.

    Databind it with an objectdatasource or some other data source

    In the select parameters you should set the query parameter to come from a querystringparameter so that the search terms are in the querystring of your page url

    For the search button code you issue a command like this:

    string Query = HttpUtility.UrlEncode(SearchTextBox.Text);
    Response.Redirect(string.Format("~/searchresults.aspx?query={0}", Query);

    then when you press the back button you are returning to a page that hasn't expired


Page 1 of 1 (9 items)