问题描述
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 + "'"
......