欢迎使用会话在每个页面上显示的消息

时间:2011-10-12 17:02:29

标签: vb.net session

在我的VB.net网站上使用会话变量已经很好了。我只需要在每一页都得到欢迎信息。我将代码和文本框放在我的母版页中以允许消息留在那里,但是当我点击其他页面时,带有用户名的标签会消失。

会话仍然存在,因为我已将其设置为显示文本框,以便在会话被销毁或由于某种原因不可用时输入您的代码。

有人能告诉我我的代码的哪一部分不允许用户会话的First_Name和Last_Name出现?问题出现在Page_Load中,但我想我会抛出整个master.vb文件来显示我到目前为止的所有内容。

Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim FirstName As String = String.Empty
    Dim LastName As String = String.Empty

    If Session("IB") Is Nothing Then
        IBText.Visible = "True"
        IBTextBox.Visible = "True"
        IBTextBoxButton.Visible = "True"
    Else
        Session("First_Name") = FirstName
        Session("Last_Name") = LastName
        IBText.Visible = "False"
        IBTextBox.Visible = "False"
        IBTextBoxButton.Visible = "False"
        lblIB.Visible = "True"
        lblIB.Text = "Welcome, " + Session("First_Name") + " " 
        + Session("Last_Name") + "."
    End If
End Sub


Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles IBTextBoxButton.Click
    Session("IB") = IBTextBox.Text
    Dim IB As String = Session("IB")
    'Response.Redirect(Request.RawUrl + "&IB=" + Session("IB"))
End Sub
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles 
CustomValidator1.ServerValidate

    Dim FirstName As String = String.Empty
    Dim LastName As String = String.Empty

    If GetAccountName(args.Value, FirstName, LastName) Then
        Session("First_Name") = FirstName
        Session("Last_Name") = LastName

        IBText.Visible = "False"
        IBTextBox.Visible = "False"
        IBTextBoxButton.Visible = "False"
        lblIB.Visible = "True"

        lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name")
        + "."
        args.IsValid = True
    Else
        args.IsValid = False
    End If
End Sub


Private Function GetAccountName(ByVal baccount As String, ByRef FirstName As String, 
ByRef LastName As String) As Boolean
    Dim sql As String = "select baccount, First_Name, Last_Name" & _
        " from IB inner join IB_BUISNESS_INFORMATION ON 
        (IB.IB_ID =  IB_BUISNESS_INFORMATION.IB_ID)" & _
        " where baccount = @baccount"
    Using conn As New 
     SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings
     ("IBConnectionString").ConnectionString)
        Using cmd As New SqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@baccount", baccount)
            conn.Open()
            Using rdr As SqlDataReader = cmd.ExecuteReader
                If (rdr.Read) Then
                    FirstName = rdr("First_Name").ToString()
                    LastName = rdr("Last_Name").ToString()
                    Return True
                Else
                    Return False
                End If
            End Using
        End Using
    End Using
End Function
End Class

1 个答案:

答案 0 :(得分:1)

如果您的会话值,则会使用String.Empty覆盖它:

 Dim FirstName As String = String.Empty
 Dim LastName As String = String.Empty
 '..... '
 Session("First_Name") = FirstName
 Session("Last_Name") = LastName

删除最后两行以及变量FirstNameLastName,因为它们无论如何都是不必要的。

此外,Visible是布尔属性,而不是String

类型
IBText.Visible = "False"

应该是

IBText.Visible = False

我建议设置Option Strict On,因为它不易出错。

你可能会遇到很多例外。但是通过更正代码,您将看到强类型的含义以及编译器已解释您的代码的程度(可能不正确但可能很慢)。为什么不直接告诉他你想要什么?! Option Strict Off是Microsoft帮助VB6程序员迁移到.NET的方式,但应该避免使用它。你的代码永远不会用C#编译。

以下是关于此主题的其他一些想法+ Option Explicithttp://www.codinghorror.com/blog/2005/08/option-strict-and-option-explicit-in-vbnet-2005.html