在子页面中访问母版页会话

accessing master page session in child pages

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

我在 student.master 页面中有此代码

然后我希望 session("StuID") 也用在 chlid 页面中....我在子页面中编写了以下代码 student.aspx-

If Not IsPostBack Then

        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

但我无法使用此 code.Can 在子页面中使用会话这里 code.Can 我得到任何帮助吗??

每个用户只有一个会话对象。您可以从任何页面访问此数据。尝试直接从会话中使用学生姓名。您不需要任何母版页

       .....
       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 + "'"
       ......