Scenario:
In this example I will explain how to find a control in a Master page and change its properties.
Suppose there is a hyperlink in a Master Page and in button click of webform that uses that Master Page, I want to hide that hyperlink.
HTML CODE (MasterPage.master):
<%@ Master Language="C#"
AutoEventWireup="true"
CodeFile="MasterPage.master.cs"
Inherits="MasterPage"
%>
<!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></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx">Default</asp:HyperLink><br /><br />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
HTML CODE (Default.aspx):
<%@ Page Title="" Language="C#"
MasterPageFile="~/MasterPage.master"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="Default"
%>
<asp:Content ID="Content1"
ContentPlaceHolderID="head"
runat="Server">
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Content>
C# Code (Default.aspx.cs):
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void Button1_Click(object
sender, EventArgs e)
{
HyperLink
hl = (HyperLink)Page.Master.FindControl("HyperLink1");
hl.Visible = false;
}
No comments:
Post a Comment