2013-06-06 63 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