MasterPage - acessando métodos e label de uma filha

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > Dicas avançadas >

MasterPage - acessando métodos e label de uma filha

Previous pageReturn to chapter overviewNext page

MinhaMaster.master

 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MinhaMestre.master.cs" Inherits="MinhaMestre" %>

 

<!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:Label ID="lblTitulo" runat="server" Text="Label"></asp:Label> <------------ AQUI

      <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

       

      </asp:ContentPlaceHolder>

  </div>

  </form>

</body>

</html>

 

MinhaMaster.master.cs

 

using System;

 

public partial class MinhaMestre : System.Web.UI.MasterPage

{

  public void MudarLabel(string titulo)

   {

       lblTitulo.Text = titulo;

   }

}

 

 

Default.aspx

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MinhaMestre.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">

  <br />

  <br />

  <asp:Button ID="Button1" runat="server" Text="Mudar label da master"

  onclick="Button1_Click" Width="173px" />

  <br />

  <br />

  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</asp:Content>

 

Default.aspx.cs

 

using System;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

  protected void Button1_Click(object sender, EventArgs e)

   {

       ((MinhaMestre)this.Master).MudarLabel("Este label está na master");

 

      Label localLabel = (Label)Master.FindControl("lblTitulo");

       Label1.Text = localLabel.Text;

   }

}