anonymous
anonymous

Reputation: 19

c# asp.net master page get a href and set visible to false

I have an a href link in my user.master page, but I'm unable to find that control in my user.master.cs. How do I do so? I tried using master and find control, but it says object reference is not instance to an object or it does not work. Please help, thanks.

user.master

<a href="ViewPremiumPlans.aspx" id="showUpgradeLink" class="btn-light btn-sm">Upgrade</a>

user.master.cs (first try)

var masterPage = Master;
 if (masterPage != null)
 {
     masterPage.FindControl("showUpgradeLink").Visible = true;
 }

(second try)

this.Master.FindControl("showUpgradeLink").Visible = false;

Upvotes: 0

Views: 510

Answers (2)

Jon P
Jon P

Reputation: 19772

You need to add runat="server" without that it is not a server control.

<a href="ViewPremiumPlans.aspx" id="showUpgradeLink" runat="server" class="btn-light btn-sm">Upgrade</a>

You then access it in the master (user.master.cs) page directly

showUpgradeLink.visible = false;

If you want to expose it to child pages add a public property to user.master.cs

public HtmlGenericControl UpgradeLink { get { return showUpgradeLink; } };

In your child aspx pages where you want to access the control/property add:

<%@ MasterType  virtualpath="~/Path/To/user.master" %>

Then in the child .cs pages you can use:

Page.Master.UpgradeLink.visible = false;

Upvotes: 0

Edgar Magallon
Edgar Magallon

Reputation: 559

Did you put runat="server" on the a tag? For example:

<a href="ViewPremiumPlans.aspx" id="showUpgradeLink" runat="server" class="btn-light btn-sm" >Upgrade</a>

Upvotes: 3

Related Questions