ASP.NET MVC 动态设置模板

在页面中添加如下代码(Page_PreInit事件代码中动态设置模板,前提是在页面Action中将用户信息存到ViewData["USER"]了):

    <script runat="server">
        //动态设置模板
        protected void Page_PreInit(object sender, EventArgs e)
        {
            switch (ViewData.Eval("USER.roleid").ToString())
            {
                case "1": this.Page.MasterPageFile = "~/Views/Shared/SuperAdmin.Master";
                    break;
                case "2": this.Page.MasterPageFile = "~/Views/Shared/Manager.Master";
                    break;
                default: this.Page.MasterPageFile = "~/Views/Shared/Default.Master";
                    break;
            }
        }
    </script>

posted @ 2010-07-27 22:00  志在必得Shaun  阅读(638)  评论(0编辑  收藏  举报