How to asign text to a label in master page from content page?

Last post 01-15-2006 9:24 AM by XIII. 4 replies.

Sort Posts:

  • How to asign text to a label in master page from content page?

    01-15-2006, 7:40 AM
    • Participant
      1,873 point Participant
    • aspfun
    • Member since 04-15-2004, 9:05 PM
    • Posts 756
    How to asign text to a label in master page from content page?
  • Re: How to asign text to a label in master page from content page?

    01-15-2006, 7:48 AM
    • All-Star
      46,040 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    your Master page be it MasterPage.master could expose a property which wraps to the Label's (Label1) Text property.

    Public Property LabelText() As String
            Get
                Return Label1.Text
            End Get
            Set(ByVal value As String)
                Label1.Text = value
            End Set
        End Property

    Content page would then need to have @MasterType directive so that the property could be accessed via Master in a typed manner.

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" title="Untitled Page" %>

    <%@ MasterType VirtualPath="~/MasterPage.master" %>

    Then, code on content page could just do:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Master.LabelText = "This text goes to the Label on master page"
            End If
        End Sub

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: How to asign text to a label in master page from content page?

    01-15-2006, 7:51 AM
    • All-Star
      122,875 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,604
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    the Page class has a Master property that you can use.

    I created this little sample for you:

    The master page:

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

    <!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:contentplaceholder id="ContentPlaceHolder1" runat="server">
            </asp:contentplaceholder>
            <asp:Label ID="Label1" runat="server" Text="Label on the Master Page"></asp:Label></div>
        </form>
    </body>
    </html>

    The webform:

    <%@ Page Language="C#" MasterPageFile="~/Master pages/SetLabelsTextFromContentPage.master" AutoEventWireup="true" CodeFile="SetLabelsTextFromContentPage.aspx.cs" Inherits="Master_pages_SetLabelsTextFromContentPage" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Put information on Master Page" />
    </asp:Content>

    and the codefile for the webform:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Master_pages_SetLabelsTextFromContentPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            ((Label)Master.FindControl("Label1")).Text = TextBox1.Text;
        }
    }

    The most important thing happens in this line:

    ((Label)Master.FindControl("Label1")).Text = TextBox1.Text;

    With Master.FindControl("Label1")  I'm able to find the control. The (Label)  casts the control that FindControl gave to a Label control. After that I can set the Text property of the found control equal to the text that I typed into TextBox1 on my webform.

    I hope this clarifies things for you.

    Grz, Kris.

  • Re: How to asign text to a label in master page from content page?

    01-15-2006, 9:06 AM
    • Participant
      1,873 point Participant
    • aspfun
    • Member since 04-15-2004, 9:05 PM
    • Posts 756

    Thank you of all.

    I use VB. So I convert to

    CType(Master.FindControl("Label1"), Label).Text = "ok"

  • Re: How to asign text to a label in master page from content page?

    01-15-2006, 9:24 AM
    • All-Star
      122,875 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,604
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    actually using DirectCast instead of CType would be more performant.

    I admit that I didn't know about the MasterType directive that joteke used. So I guess we both learned something new today Smile [:)].

    Grz, Kris.

Page 1 of 1 (5 items)