2010-12-08 56 views
2

我對使用會話狀態仍然陌生,我想根據數據庫錶轉換頁面名稱和整數 然後函數比較「X」和「Y」來檢查用戶是否有有權查看此頁面使用會話狀態不能正常工作

我知道這是不是管理網站的安全性的最佳方式,但很喜歡

我做了什麼錯「如何使用會話培訓」

Partial Class advancedsearch 
Inherits System.Web.UI.Page 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Try 
     Label1.Text = Session("username").ToString 

     Label3.Text = Session("role").ToString 
     Label4.Text = System.IO.Path.GetFileName(Request.Url.ToString()) 


    Catch ex As Exception 
     Response.Redirect("login.aspx") 
    End Try 

    If Label1.Text = "" Then 
     Response.Redirect("login.aspx") 

    End If 
Dim x As Integer = Int32.Parse(Label3.Text) 

    Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString) 

    If x < y Then Response.Redirect("login.aspx") 


End Sub 


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Response.Redirect("default.aspx") 
End Sub 
End Class 

回答

1

嘗試將比較部分放在預渲染完成e

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete 
    Dim x As Integer = Int32.Parse(Label3.Text) 

    Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString) 

    If x < y Then Response.Redirect("login.aspx") 

End Sub