日期:2014-05-17  浏览次数:20433 次

BasePage.cs如何控制权限?
咨询个问题:我建立了一个BasePage.cs文件,在里面做权限控制,我是在构造函数里面做文章还是在OnInit方法里面做文章。代码如下:
C# code
    public class BasePage : System.Web.UI.Page
    {
        public BasePage()
        {
            this.Load += new System.EventHandler(Rank);
        }

        private void Rank(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("~/MyLogin.aspx");
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("~/MyLogin.aspx");
            }
            base.OnInit(e);
        }
    }


------解决方案--------------------
C# code

//需检查权限的页面
public partial class Questions : MyPage
{
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((SiteMaster)this.Master).CheckPermition(OES2.MyAccount.AccountRole.教师, "", 6);
            }
        }
}
//模板页
public void CheckPermition(MyAccount.AccountRole BaseRole,string Title,int selectedIndex)
        {
            ShowTitle(Title,selectedIndex);
            if (BaseRole == MyAccount.AccountRole.未定义)
            {
                MainContent.Visible = true;
                HasErr = false;
                ErrorState = "";
                if (MyAccount.IsAccessed(Common.AccountSession, MyAccount.AccountRole.登录用户))
                {
                    div_unreged.Style.Add(HtmlTextWriterStyle.Display, "none");
                    div_reged.Style.Add(HtmlTextWriterStyle.Display, "inline");
                }
                else
                {
                    div_unreged.Style.Add(HtmlTextWriterStyle.Display, "inline");
                    div_reged.Style.Add(HtmlTextWriterStyle.Display, "none");
                }
            }
            else
            {
                if (MyAccount.IsAccessed(Common.AccountSession, BaseRole))
                {
                    MainContent.Visible = true;
                    HasErr = false;
                    ErrorState = "";
                    div_unreged.Style.Add(HtmlTextWriterStyle.Display, "none");
                    div_reged.Style.Add(HtmlTextWriterStyle.Display, "inline");
                }
                else
                {
                    MainContent.Visible = false;
                    HasErr = true;
                    ErrorState = "-1";
                    div_unreged.Style.Add(HtmlTextWriterStyle.Display, "inline");
                    div_reged.Style.Add(HtmlTextWriterStyle.Display, "none");
                }
            }
        }

------解决方案--------------------
OnInit 就行了
------解决方案--------------------
OnInit 里进行控制 足以