Hide Link Button on Master Page

  • Thread starter Thread starter Mick Walker
  • Start date Start date
M

Mick Walker

Dave said:
As the title describes, I'm trying to hide a link button on the master
page if a person isn't logged in. Testing whether or not a person is
logged in isn't the problem. It's refrencing that control's
properties to change it to true if they are.

Any discriptive help would be apreciated.
I personally would expose a public property on the masterpage. i.e

Public Property HideLink() as Boolean
Get
return lnkButton.Visable
End Get
Set(ByVal value As Boolean)
lnkButton.Visable = value
End Set
End Property

That way you can show/hide the button at will.
 
As the title describes, I'm trying to hide a link button on the master
page if a person isn't logged in. Testing whether or not a person is
logged in isn't the problem. It's refrencing that control's
properties to change it to true if they are.

Any discriptive help would be apreciated.
 
I personally would expose a public property on the masterpage. i.e

Public Property HideLink() as Boolean
Get
return lnkButton.Visable
End Get
Set(ByVal value As Boolean)
lnkButton.Visable = value
End Set
End Property

That way you can show/hide the button at will.



Thanks for the Quick response Mick! Ok...Now you get to see how much
of a Novice I really am. Where would I place that function on the
MasterPage?
 
I personally would expose a public property on the masterpage. i.e

Public Property HideLink() as Boolean
Get
return lnkButton.Visable
End Get
Set(ByVal value As Boolean)
lnkButton.Visable = value
End Set
End Property

That way you can show/hide the button at will.



I should have pointed out that this is C#.
 
Dave said:
As the title describes, I'm trying to hide a link button on the master
page if a person isn't logged in. Testing whether or not a person is
logged in isn't the problem. It's refrencing that control's
properties to change it to true if they are.

Any discriptive help would be apreciated.


((Button)this.Master.FindControl("MyLinkButton")).Visible = false;
 
Dave said:
I should have pointed out that this is C#.
Hi Dave,

With a little modification this code can apply to C# as well. I'll walk
you though it and also I will email you the zipped project to your email
address.

Lets start with the master page, I'll call it Site.Master, here is the
markup:

<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="WebApplication1.Site" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server">My Link
Button</asp:LinkButton>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Now this is the code-behind for the masterpage:

using System;


namespace WebApplication1
{
public partial class Site : System.Web.UI.MasterPage
{
public bool HideLink
{
get
{
return LinkButton1.Visible;
}
set
{
LinkButton1.Visible = value;
}
}

}
}


All it contains is the property HideLink.

Now create a content page, call it Default.aspx and select Site.Master
as the Master Page. Here is the markup:

<%@ Page Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1.Default" Title="Untitled Page" %>
<%@ MasterType virtualPath="~/Site.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:Button ID="Button1" runat="server" Text="Show"
OnClick="Button1_Click" /><br />
<br />
<asp:Button ID="Button2" runat="server" Text="Hide"
OnClick="Button2_Click" />

</asp:Content>


It simply contains 2 buttons, one to hide the link button, one to show
the link button. Notice the:
<%@ MasterType virtualPath="~/Site.master"%>
This allows us to reference Public methods of our masterpage from
content pages. Ok onto the code behind:

using System;


namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{

protected void Button1_Click(object sender, EventArgs e)
{
Master.HideLink = true;
Response.Write("The Visability of the Link is currently: "
+ Master.HideLink.ToString());
}

protected void Button2_Click(object sender, EventArgs e)
{
Master.HideLink = false;
Response.Write("The Visability of the Link is currently: "
+ Master.HideLink.ToString());
}
}
}

This is simple, depending on the button clicked all the code does is
alter the Visable Status of the linkbutton on the master page, by using
the public property we just defined. Because we also created a "Get"
member, we can retrived the current status of the LinkButton at any
time, as I do, and I write the status to the page.

I hope this helps, I'll email you over the zipped project now.

Regards
Mick
 
As the title describes, I'm trying to hide a link button on the
master page if a person isn't logged in. Testing whether or not a
person is logged in isn't the problem. It's refrencing that
control's properties to change it to true if they are.

Any discriptive help would be apreciated.

If you're using MembershipProvider, you can use asp:LoginView, like
this (it's really quick and easy):

<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<!-- here put everything for logged in user -->
</LoggedInTemplate>
<AnonymousTemplate>
Please login.
</AnonymousTemplate>
</asp:LoginView>
 
Dave said:
As the title describes, I'm trying to hide a link button on the master
page if a person isn't logged in. Testing whether or not a person is
logged in isn't the problem. It's refrencing that control's
properties to change it to true if they are.

Any discriptive help would be apreciated.

Just do it in the Page_Load event of the master page.

Referencing controls of the master page from within your page is possible,
but it is bad design (agains OOP principles).
 
Referencing controls of the master page from within your page is possible,
but it is bad design (agains OOP principles).

Utter rubbish! A MasterPage is just a UserControl - there's nothing
particularly special about it...

Referencing its controls is no different from referencing controls anywhere
else...
 
Mark said:
Utter rubbish! A MasterPage is just a UserControl - there's nothing
particularly special about it...

Referencing its controls is no different from referencing controls
anywhere else...

Referencing controls on user controls (if you don't do it through a property
or method) is, in my opinion, also bad design.

Changing the internal design of the user control shouldn't break code
elsewhere in the application.
 
Back
Top