PostBack上的Asp.NET MasterPage会话变量为NULL

时间:2014-08-19 13:07:51

标签: asp.net vb.net null postback session-variables

我搜索了与此类似的多个问题,但未能提出解决方案。在我的主页中,我有一个位于页面标题中的搜索文本框。单击搜索按钮时,我在on_click事件中设置了session variable = textbox2.text。然后我有一个单独的页面," Page.aspx"继承了MasterPage。当我在Page.aspx上尝试搜索某些内容时出现问题,因为onPostBack,它没有在文本框中看到文本。我试图找出如何设置会话("搜索")= Textbox2.text而postback = true。这是我的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

这是我和#34; 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

1 个答案:

答案 0 :(得分:0)

您的问题的直接答案是将Page.opx Page_Load事件中的逻辑移动到LoadComplete事件中。单击按钮后将触发“加载完成”事件,以便设置会话变量。

更好的方法是停止使用会话传递搜索值。

一种方法是:

  • 将Page.opx中的Page_Load逻辑移至Load Complete事件。
  • 从Master1传递搜索参数作为查询字符串值而不是会话。请务必对此进行编码,因为它可能包含任何字符。 Instructions
  • 将Master2的搜索逻辑更改为与Master1相同。这可以重构为常用方法。结果是,即使您已经在Page.aspx上,也可以重定向到Page.aspx。
  • 确保在按钮单击事件中执行重定向时结束响应。 Me.Response.Redirect(theurl,True)