0

I have searched over multiple questions similar to this one but have not been able to come up with a solution. In my Masterpage I have a search textbox that is located in the header of the page. When the search button is clicked, I set the session variable = textbox2.text in the on_click event. Then I have a separate page, "Page.aspx" that inherits that MasterPage. The problem comes when I am trying to search for something while i'm on Page.aspx because onPostBack, it is not seeing the text in the textbox. I am trying to figure out how I can set session("search") = Textbox2.text while postback = true. Here is the code from my MasterPage...

Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
    ViewState("Search") = TextBox2.Text
    Session("Search") = TextBox2.Text

    Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Sprayer_Parts_CatalogConnectionString").ConnectionString.ToString)
    Dim sql As New SqlDataSource
    Dim sql3 As New SqlCommand
    Dim sql4 As New SqlCommand
    Dim str2 As String
    Dim str As String
    sql4.Connection = Conn
    sql3.Connection = Conn
    Conn.Open()

    sql3.Parameters.AddWithValue("@Search", TextBox2.Text)
    sql4.Parameters.AddWithValue("@Search", TextBox2.Text)
    sql3.CommandText = "Select Category From SubCategory WHERE Category Like '%' + @Search + '%'"
    sql4.CommandText = "Select Type From SubCategory WHERE Type LIKE '%' + @Search + '%' OR Category LIKE '%' + @Search + '%'"

    str = sql4.ExecuteScalar().ToString
    'Response.Redirect("~/Parts_Catalog.aspx?")
    Conn.Close()

End Sub

And here is the code from my "Page.aspx.vb"

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Dim url1 As String = Request.QueryString("Category")
    Dim url As String = Request.QueryString("Item")
    Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Sprayer_Parts_CatalogConnectionString").ConnectionString.ToString)
    Dim sql As New SqlDataSource
    Dim sql3 As New SqlCommand
    sql3.Connection = Conn
    Dim UrlStr As String
    Dim Search As String = CType(Session.Item("Search"), String)

    If Not Search = "" Then
        FormView1.DataSourceID = "SqlDataSource4"

        Dim LV As ListView = FormView1.FindControl("ListView1")
        LV.DataSourceID = "SqlDataSource4"
        LV.DataBind()

    End If

    If url1 = "Spray Guns" Then

        Response.Redirect("~/Product.aspx?Product=" + url1)

    End If
    If IsPostBack Then

        If Search = "" Then
            Conn.Open()
            sql.SelectParameters.Add("@Item", url)
            sql3.Parameters.AddWithValue("@Item", url)
            sql3.CommandText = "Select Type From SubCategory Where Category = @Item"
            sql.SelectCommand = sql3.CommandText
            UrlStr = sql3.ExecuteScalar.ToString

            If UrlStr = "" Then
                Response.Redirect("~/Product.aspx?Product=" + url)
            End If

            Conn.Close()
            Response.Write("Param is :" + url1)
        End If
    End If


End Sub
user3841709
  • 386
  • 6
  • 28
  • It is not clear what the flow is. Should the ImageButton2_Click redirect to the Page.aspx, passing in the Category and Type as queryString values? – Gridly Aug 19 '14 at 14:31
  • That is essentially what is taking place yes. Because once I pass the session value through, I query that string value in one of my SqlDataSources. For right now though, I just simply need to know how to get the value of the textbox while IsPostBack is true on Page.aspx. I have tested actually passing it into a query and it works, just not when I can't get the value of the Session("search") on IsPostBack – user3841709 Aug 19 '14 at 14:36
  • So, When I first load the Page.aspx page, there is a textbox in the Header of that Page. The Header and textbox are actually coded on the MasterPageFile. So when text is typed into that textbox and the search button is clicked, I am trying to assign Session("search") = Textbox2.text in the masterpage file since I don't have access to the textbox from Page.aspx. Then I am trying to read the Session("search") on Page.aspx to get the value. Does that make sense? – user3841709 Aug 19 '14 at 14:39
  • If page.aspx is being displayed and the user enters a search value and clicks the button then the session value for "Search" will not be set because the Page's Page_load event fires before the button's Click event. If the value is changed and the button clicked again then the prior value should show up because that value will still be in the session. – Gridly Aug 19 '14 at 15:03
  • The session is not the correct way to pass values between pages. This leads to bugs because the value remains in Session until explicitly removed. As the user moves back and forth through the pages you will be triggering logic because of residual data. – Gridly Aug 19 '14 at 15:06
  • Yea that's correct. Is There a way to set the value for "Search" so that it does assign a value on the first postback? – user3841709 Aug 19 '14 at 15:10
  • I have already briefly looked for a different solution other than using session but just very little. I saw some suggestions elsewhere on using viewstate but I'm not if that will suffice or not. Could you possibly lead me in the correct direction of how to manage passing this value on postback – user3841709 Aug 19 '14 at 15:15
  • Is the design to use this master page with many pages and when the search is performed the user is redirected to the Page.aspx to see the search results? – Gridly Aug 19 '14 at 15:25
  • Well I have many other pages along with the "Page.aspx" page. All of my other pages are using a separate masterpage that we will call "MasterPage1". "Page.aspx" is using "MasterPage2". I currently have it set up this way because of different and menu's that need to be displayed soley on Page.aspx. I have the search textbox on both masterpage's and everything works fine from the other pages since I am redirecting them to Page.aspx. The sole design for MasterPage2 is to be used with Page.aspx only. Therefore I run into the postback problem. And yes they should redirect to Page.aspx – user3841709 Aug 19 '14 at 15:30
  • So the design of this masterpage is just for 1 page and when something is typed into the search box, results display on Page.aspx – user3841709 Aug 19 '14 at 15:34

1 Answers1

0

The direct answer to your question is to move the logic in the Page.aspx Page_Load event into the LoadComplete event. The Load Complete event fires after the button click so the session variable will be set.

A better approach would be to stop using the session to pass search value.

One approach is:

  • Move the Page_Load logic in Page.aspx to the Load Complete event.
  • From Master1, pass the search param as a query String value instead of Session. Be sure to encode this because it could have any characters. Instructions
  • Change the search logic for Master2 to be the same as Master1. This could be refactored to a common method. The result is that you do the redirection to Page.aspx even when you are already on Page.aspx.
  • Be sure to end the response when doing the redirection in the button click events. Me.Response.Redirect(theurl, True)
Community
  • 1
  • 1
Gridly
  • 938
  • 11
  • 13
  • I believe I have fixed it, at least a temporary fix. I placed the code in the Load Complete Page Event like you suggested and I am now passing the textbox value as a query string. Would you suggest doing the same thing for this on the other Master Page as well? – user3841709 Aug 19 '14 at 17:07
  • Yes, making them both the same would make maintenance easier – Gridly Aug 19 '14 at 17:28
  • Ok, Thank you so much for all your help. I believe this fix should work great. – user3841709 Aug 19 '14 at 17:30