ASP.net access a master page variable through content page

你说的曾经没有我的故事 提交于 2019-12-30 05:55:12

问题


I have a master page:

<%@ Master Language="C#" AutoEventWireup="true" Codefile="AdminMaster.master.cs" Inherits="AlphaPackSite.MasterPages.AdminMaster" %>

Then I have a public variable:

public partial class AdminMaster : System.Web.UI.MasterPage
{
    protected bool blnShowDialogue = false;

In my content page I would like to set this variable:

blnShowDialogue = true;

So that in my master page I can have the code:

    $(function() {
    <%if(blnShowDialogue == true){%>
        $("#dialog").dialog();
    <% } %>
    }

Does this make sense? When I try combinations of Master.blnShowDialogue, or blnShowDialogue = , etc etc nothing seems to work.

The name 'blnShowDialogue' does not exist in the current context


回答1:


Use @MasterType directive, as explained here:

http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx




回答2:


You need to cast the Master page to the actual type.

((AdminMaster)Master).blnShowDialogue = "Foo";

Otherwise Master will simply be referring to the base class Master - you're trying to access a property in your actual class which derives from the Master class.

The error you are getting is because a property called blnShowDialogue does not exist in the class System.Web.UI.MasterPage - which makes sense, as you're not telling it which specific MasterPage instance you are trying to refer to.

Hope that helps.



来源:https://stackoverflow.com/questions/3651326/asp-net-access-a-master-page-variable-through-content-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!