从内容页设置母版页上的属性值
每次加载页面时,我都需要将数据传递给母版页中的变量。
我在每个内容页面上设置了一个RequiredRoles 字符串[],定义了访问该页面所需的角色。
在我的母版页上,我有一种方法可以获取该数组,并检查当前用户是否属于这些角色中的一个或多个。
我将如何处理这个问题? 我基本上希望每个页面都定义一个 String[]RequiredRoles,母版页将在每次调用时加载它并检查用户是否处于这些角色。
I need to pass data to a variable in my master page each time a page is loaded.
I have a string[] of RequiredRoles that I set on each content page defining what roles are required to access that page.
On my master page, I have a method that takes this array, and checks to see if the current user is in one or more of those roles.
How would I go about managing this? I basically want each page to have a String[] RequiredRoles defined, and the master page will load this on each call and check to see if the users are in those roles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
CType(Master.FindControl("lblName"), Label).Text = txtId.Text
CType(Master.FindControl("pnlLoginned"), Panel).Visible = True
CType(Master.FindControl("lblName"), Label).Text = txtId.Text
CType(Master.FindControl("pnlLoginned"), Panel).Visible = True
在母版页中创建一个属性,然后从内容页访问它:
母版页:
内容页:
Create a property in your master page and you access it from content page:
Master page:
Content Page:
我会为所有内容页面创建一个基类,例如:
然后我让每个页面都继承自 BasePage,并且每个页面都定义一个RequiredRoles
这具有整洁和干燥 OnLoad 处理程序代码的优点。 每个继承自 BasePage 的页面都需要定义一个“RequiredRoles”,否则无法编译。
I'd go by creating a base class for all the content pages, something like:
And then I make every page inherits from BasePage, and each defining a RequiredRoles
This has the advantage of cleanliness and DRY-ing the OnLoad handler code. And every page which inherits from BasePage are required to define a "RequiredRoles" or else it won't compile.
将 Page.Master 类型转换到您的母版页,以便您执行以下操作:
Typecast Page.Master to your master page so that you are doing something like:
将页面指令添加到您的子页面:
然后将属性添加到您的母版页:
您可以像这样访问此属性:
Add page directive to your child page:
Then add property to your master page:
You can access this property like this: