Search code examples
asp.netstore

Store search results GridView


I have the following GridView:

asp:GridView ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="ID" 
    gridlines="None"
    cellpadding="15"
    width="980px"   
    ItemStyle-backcolor="#ebecf0"
    AlternatingItemStyle-backcolor="#ebecf0" 
    AllowPaging="True" 
    PageSize="4" 
    onpageindexchanging="GridView1_PageIndexChanging" 
    datasourceid="SqlDataSource2"
    >

And this select command:

"SELECT * FROM [tbl_Project] INNER JOIN tbl_Cat      
ON tbl_Project.CatID = tbl_Cat.Cat_ID 
INNER JOIN tbl_Klant   
ON tbl_Project.KlantID = tbl_Klant.Klant_ID 
WHERE (([Titel] LIKE '%' + @Titel + '%') 
AND  ([CatID] = CASE WHEN @CatID = -1 THEN [CatID] ELSE @CatID END) 
AND ([Bedrijf] LIKE '%' + @Bedrijf + '%') 
AND ([Website] LIKE '%' + @Website + '%'))" 

This allows the user to search records in the database. In the GridView1 I havea detail button:

 <asp:LinkButton ID="klant" runat="server"
    Text='<%#Eval("Bedrijf") %>'
    PostBackUrl='<%# "klant_wijzigen.aspx?Klant_ID="+Eval("Klant_ID").ToString()%>'>
    </asp:LinkButton>

this takes the user to a new page with the details of that certain subjec(based on ID)

QUESTION

But when the user clicks on "back" the seaarch results are cleared. How can I store those searched records and display them on PageLoad.

I have tried it trough cookies and Session but it dindt work.

EDIT

my sesion attempt:

Session("Test") = GridView1
GridView1 = Nothing

' Retrieve GridView from Session
GridView1 = DirectCast(Session("Test"), GridView)
GridView1.DataBind()

Solution

  • Firstly try to use OnClientClick event of LinkButton instead of PostBackUrl.

    When you will click on LinkButton then on it's OnClientClick event do some steps.

    1. Store the values of the parameters that you are passing in Select Query in Session.

      It may come from anywhere like from Textbox's,Label's etc etc.If it's come from controls then store in session like

      Session("Bedrijf") = Bedrijf.Text
      

      Here I am assuming that Bedrijf value comes from a TextBox of ID Bedrijf.Text. So I save that value into a session. Just store the others controls value as well.

      Note: only store those values that has been used into your select query.

      After storing all values into session. You just redirect it to the next page.

    2. On page2 do what ever you want.

    3. On back button click also set the pagename into your session variables.Right now you are on page2.aspx so set it's name in session like

      Session("prevpagename") = "Page2"
      
    4. After hitting back button it will redirect to the same page like page 2 to page 1, now here on page 1 Page_Load event bind your grid view again like

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          If Not Page.IsPostBack Then
            // Here firstly check the session variables for `pagename`
            If Session("prevpagename") = "Page2" Then
            // then directly assign the values of the parameters that you have store in session in select query.
            // After retrieving the values from the database filter by the parameters you have passed bind your `Grid View` again like
            gridview.DataSource = reader2
            gridview.DataBind()
            // Here reader2 is having the all return data that comes from your select query.You may save them on `DataTable`,`DataSet` as well and directly assign it to `DataSource` event of `GridView`.
            Else
            // another code.
            End If
          End If
      End Sub
      

    You have to follow this type of scenario.

    Hope you understand and works for you.