How to control elements on a asp.net master page from child page

前端 未结 5 1708
野趣味
野趣味 2020-12-08 12:33

I have a few pages on my asp.net website that I would like to turn off a control on the master page. Is there a way to communicate with the master page from a child page?

5条回答
  •  不思量自难忘°
    2020-12-08 12:56

    Easiest way is to setup a property on your master page that handles the on/off functionality when called. Then in your child page set the MasterType directive to get a strongly typed reference to your master page to bypass the need to cast.

    Your child page would have:

    <%@ MasterType VirtualPath="~/Site1.Master" %>
    

    And to call the property of the master page:

    Master.MyLabel = false; // or true
    

    So on your master you could have:

    public bool MyLabel
    {
        get
        {
            return masterLabel.Enabled;
        }
        set
        {
            masterLabel.Enabled = value;
        }
    }
    

提交回复
热议问题