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>



你可能感兴趣的:(asp.net)