开发者

Welcome message to display on every page using session

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 th开发者_运维技巧e 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜