如何解决在子页面中访问母版页会话
Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) 处理 Me.Load
lblSTuName.Text = "Welcome! " + Session("StuID")
If con.State = ConnectionState.Closed Then
con.open()
End If
cmd = New OleDbCommand
cmd.Connection = con
cmd.CommandText = "select [Image] from tblStudent where Uname='" + Session("StuID") + "'"
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader()
If dr.Read() Then
stuImage.ImageUrl = dr("Image")
Else
stuImage.ImageUrl = nothing
End If
con.Close()
End Sub
然后我希望 session("StuID") 也用于 chlid 页面....我在子页面 student.aspx- 中编写了以下代码-
如果不是 IsPostBack 那么
Dim stname As Label = TryCast(Master.FindControl("lblSTuName"),Label)
stuN = stname.Text
If con.State = ConnectionState.Closed Then
con.open()
End If
cmd = New OleDbCommand
cmd.Connection = con
cmd.CommandText = "select [RollNo],[CourceName] from tblStudent where StuName='" + stuN + "'"
dr = cmd.ExecuteReader()
If dr.Read() Then
MsgBox("read complete")
Else
MsgBox("not success!")
End If
End If
但我无法使用此代码在子页面中使用会话。我可以获得任何帮助吗??
解决方法
每个用户只有一个会话对象。您可以从任何页面访问这些数据。尝试直接使用会话中的学生姓名。您不需要任何母版页
.....
Dim stuN = Session("StuID");
If con.State = ConnectionState.Closed Then
con.Open()
End If
cmd = New OleDbCommand
cmd.Connection = con
cmd.CommandText = "select [RollNo],[CourceName] from tblStudent where StuName='" + stuN + "'"
// or better to try
cmd.CommandText = "select [RollNo],[CourceName] from tblStudent where Uname='" + stuN + "'"
......
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。