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