2013-06-06 67 views
0

我有以下的GridView:商店搜索结果的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我havea细节按钮:

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

这将用户带到一个新的页面,某些subjec(基于ID)

问题

的细节,但是,当用户点击“返回”结果将被清除。如何存储这些搜索到的记录并将其显示在PageLoad上。

我尝试过槽曲奇和会议,但它的工作。

编辑

我使sesion尝试:

Session("Test") = GridView1 
GridView1 = Nothing 

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

通过会话是最好的方式,但不是将整个'GridView'存储到您的会话中,只需将您正在使用的参数的值存储到'where子句'中,当您返回时,只需将这些参数再次分配给您的查询并再次绑定您的网格视图。 – Rahul

+0

我该怎么做,而不是GridView的text-boxes.text?你有一个教程(不要真的像snippets) – ErikMes

+0

我没有教程,但我做了同样的事情在我手上,如果你愿意,那么我可能会给你一点help.it会很容易,还提到' vb'到你的问题标签中。 – Rahul

回答

1

首先尝试使用OnClientClick事件LinkButton而不是PostBackUrl。 当你点击LinkButton然后在它的OnClientClick事件中执行一些步骤。

1)将您在Select Query中传递的参数的值存储在Session中。
它可能来自任何地方像Textbox'sLabel's等etc.If它来自 控制则在会话存储一样

Session("Bedrijf") = Bedrijf.Text 

我在这里假设Bedrijf价值来自ID Bedrijf.Text。所以我的TextBox 将该值保存到session.Just存储其他人控制值。

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

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

2)在第2页上做任何你想要的。

3)在back button点击还设置pagename到你的session变量。现在你是page2.aspx所以设置它的名字在session

Session("prevpagename") = "Page2" 

4)打后退按钮后,它会重定向到同一页面像page 2 to page 1,现在这里page 1Page_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 retereiving 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 

你必须遵循这种类型的情景。

希望你理解并为你工作。

+0

即使.text = Null,这样做还能工作吗?他们不必填写每一个领域。 – ErikMes

+0

在'session'中存储每个值,但是当您将这些值分配给查询时,然后检查是否为'Null'。 – Rahul

+0

奥克非常感谢你!但是我迷失在第3点的第3点 – ErikMes

0

会议( “测试”)= GridView1

我觉得这是不好的,而不是保存您的选择查询中会话b4重定向,还配置详细信息页面以重定向某些查询字符串值,以便您知道必须使用selec来自会话的查询。

+0

我不是一个有SQL的英雄,如果你能把我指向正确的方向,或者关于它的一个好主意(搜索我的屁股) – ErikMes

1

尝试Cache您的表格。像Cache["SOURCE"] = YourSearchDataTable;然后由浇铸这样称呼它:

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

只是一个建议和替代会话和饼干。

+0

我将缓存放在search_click事件上吗? – ErikMes

+0

是的。你可以把它放在'onClicked'事件中,然后你可以随时找回它。 –

+0

我该如何检索它? – ErikMes

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 liek这样的:

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