我正在尝试创建以下方案......
我遇到的麻烦是用户ID和密码在提交电子邮件地址时没有保留,因此登录失败。
这是代码......
Dim userid as String
Dim password as String
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
end sub
Sub Page_Transfer()
Dim userLookup As mySource.myData = New mySource.myData()
Dim drSet1 As DataSet = userLookup.xyz("SELECT * FROM OPENQUERY (myDatabase,' SELECT userid, password, email1 FROM userInfo WHERE userid = ''" & userid & "'' AND password = ''" & password & "'' ') ")
If drSet1.Tables(0).Rows.Count < 1 Then
'Invalid Bond number
Page.ClientScript.RegisterClientScriptInclude("loginError", "scripts/loginError.js")
Else
If drSet1.Tables(0).Rows(0).Item("email1") = "" Then
'No email address
Page.ClientScript.RegisterClientScriptInclude("emailAddressSubmit", "scripts/emailAddressSubmit.js")
Else
response.redirect("home.aspx")
End If
End If
End Sub
Sub Submit_Email(sender As Object, e As System.Web.UI.ImageClickEventArgs)
'script to add email address to database
'this is where the strings are losing their values
Page_Transfer()
End Sub
Sub Submit_login(sender As Object, e As System.Web.UI.ImageClickEventArgs)
userid = Trim(useridfield.text)
password = Trim(passwordfield.text)
Page_Transfer()
End Sub
有什么想法吗?
答案 0 :(得分:3)
问题在于,每次回发时,页面都会经历一个全新的生命周期。这意味着您在一次回发期间设置的页面的任何实例变量都会在下一个回复中丢失。
如何解决这个问题?您有以下选择:
从控件(useridfield和passwordfield)再次读取用户ID和密码。由于ASP.NET控件使用视图状态在回发后保留其值,因此这些控件中的值仍应有效。
自己使用视图状态来保存变量的值,即替换
Dim userid as String
Dim password as String
与
Protected Property userid As String
Get
Return DirectCast(Me.ViewState("userid"), String)
End Get
Set
Me.ViewState("userid") = value
End Set
End Property
' same for password
但请注意,视图状态不安全,即可以由Web应用程序的熟练用户修改。因此,在为其分配邮件地址之前,请务必再次验证用户标识和密码的值(检查数据库是否匹配)。 (顺便说一下,选项1也是如此。)
将值保存在其他位置,例如Session。