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

发布于 2024-12-09 15:01:40 字数 3074 浏览 0 评论 0 原文

在我的 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

Using a session variable in my VB.net site has been figured out just fine. I just need to get my welcome message to stay put on every page. I put the code and text box in my master page to allow the message to stay there, but my label with the user's name disappears when I click on a different page.

The session is still there because I have it set up to display the textbox to enter your code if the session is destroyed or not available for some reason.

Can someone tell me which part of my code is not allowing the First_Name and Last_Name of the user's session to show up? The problem is in the Page_Load but I thought I would throw in the whole master.vb file to show everything I have so far.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-16 15:01:40

如果您的 Session 值不是什么都没有,则您将使用 String.Empty 覆盖它:

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

删除最后两行以及变量 FirstName 和 < code>LastName 因为它们无论如何都是不必要的。

此外,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

If your Session value is not nothing, you are overwriting it with String.Empty:

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

Remove the last two lines and also the variables FirstName and LastName because they are needless anyway.

Besides, Visible is a boolean property and not of type String

IBText.Visible = "False"

Should be

IBText.Visible = False

I would recommend to set Option Strict On because it's less error-prone.

You will probably get many exceptions. But by correcting your code you'll see what strong type means and how much the compiler yet has interpreted your code(maybe incorrect but likely slow). Why not telling him directly what you want?! The Option Strict Off is the way Microsoft helps VB6 programmers to migrate to .NET, but it should be avoided. Your code would never compile with C# by the way.

Here are some other thoughts about this topic+Option Explicit: http://www.codinghorror.com/blog/2005/08/option-strict-and-option-explicit-in-vbnet-2005.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文