0

我有以下 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"
    >

而这个选择命令:

"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 + '%'))" 

这允许用户在数据库中搜索记录。在 GridView1 我有一个详细信息按钮:

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

这会将用户带到一个新页面,其中包含该特定主题的详细信息(基于 ID)

问题

但是当用户点击“返回”时,搜索结果会被清除。如何存储这些搜索记录并在 PageLoad 上显示它们。

我已经通过 cookie 和 Session 尝试过它,但它不起作用。

编辑

我的会话尝试:

Session("Test") = GridView1
GridView1 = Nothing

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

4 回答 4

1

首先尝试使用OnClientClick事件LinkButton而不是PostBackUrl.

当你点击LinkButton然后它的OnClientClick事件做一些步骤。

  1. 存储您传入的参数的Select QuerySession

    它可能来自任何地方,例如 fromTextbox'sLabel's。如果它来自控件,则存储在会话中

    Session("Bedrijf") = Bedrijf.Text
    

    在这里,我假设Bedrijfvalue 来自TextBoxID Bedrijf.Text。所以我将该值保存到session. 只需存储其他控件的值即可。

    注意:仅存储已用于您的选择查询的那些值。

    将所有值存储到会话中之后。您只需将其重定向到下一页。

  2. 在 page2 上做任何你想做的事。

  3. back button单击时还将其设置pagename到您的session变量中。现在您已打开,因此page2.aspx将其名称设置session

    Session("prevpagename") = "Page2"
    
  4. 点击返回按钮后,它会重定向到同一个页面page 2 to page 1,现在在这里再次page 1 Page_Load绑定你的事件grid view

    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
    

您必须遵循这种情况。

希望您理解并为您工作。

于 2013-06-06T09:49:21.960 回答
1

试试Cache你的桌子。然后像Cache["SOURCE"] = YourSearchDataTable;这样通过铸造来调用它:

DataTable dt = (DataTable)Cache["SOURCE"];
GridView1.DataSource = dt;
GridView1.DataBind();

只是会话和 cookie 的建议和替代方案。

于 2013-06-06T09:56:56.313 回答
0

会话(“测试”)= GridView1

我认为这很糟糕,而是将您的选择查询保存在会话 b4 重定向中,还将详细信息页面配置为使用一些查询字符串值进行重定向,以便您知道必须使用会话中的选择查询。

于 2013-06-06T09:13:46.787 回答
0

我通过在 selectedindex 更改上存储会话来修复它,如下所示:

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Categorie") = DropDownList1.SelectedValue
End Sub

Protected Sub txtKlant_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Klant") = txtKlant.Text
End Sub

Protected Sub txtWebsite_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Website") = txtWebsite.Text
End Sub

Protected Sub txtTitel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Titel") = txtTitel.Text
End Sub

然后在 page_load 上调用它,就像这样:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Cookie()

    If Not Page.IsPostBack Then           
        DropDownList1.SelectedValue = Session("Categorie")
        txtKlant.Text = Session("Klant")
        txtWebsite.Text = Session("Website")
        txtTitel.Text = Session("Titel")
        GridView1.DataBind()
    End If

End Sub
于 2013-06-07T09:35:52.137 回答