使用会话在每个页面上显示欢迎消息
在我的 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的 Session 值不是什么都没有,则您将使用
String.Empty
覆盖它:删除最后两行以及变量
FirstName
和 < code>LastName 因为它们无论如何都是不必要的。此外,
Visible
是一个布尔属性,而不是String
类型,我应该
建议设置Option Strict On 因为它不太容易出错。
您可能会遇到很多例外情况。但是通过更正您的代码,您将看到强类型的含义以及编译器已解释您的代码的程度(可能不正确但可能很慢)。为什么不直接告诉他你想要什么?
Option Strict Off
是 Microsoft 帮助 VB6 程序员迁移到 .NET 的方式,但应该避免使用。顺便说一下,你的代码永远不会用 C# 编译。以下是关于此主题的一些其他想法+
Option Explicit
:http://www.codinghorror.com/blog/2005/08/option-strict-and-option-explicit-in-vbnet-2005.htmlIf your Session value is not nothing, you are overwriting it with
String.Empty
:Remove the last two lines and also the variables
FirstName
andLastName
because they are needless anyway.Besides,
Visible
is a boolean property and not of typeString
Should be
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