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