Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a Label on a Master Page from a Content Page 1

Status
Not open for further replies.

DotNetDunce

Programmer
Aug 23, 2001
47
US
I have a hidden label on the master page that I want to be able to read the text of from a content page. I've used the page.master.findcontrol("label") to find the control but I don't know how to retrieve the text from it. Would someone point me in the right direction?

Thanks!
 
Thanks for replying ca8msm!

That's what I called myself doing, but for some reason it will not populate the text. Should this work, or am I doing this wrong?

Code:
Dim Test As Label
Test = Page.Master.FindControl("lblType")
txtTest.Text = Test.Text

I'm still learning this stuff so I apologize if it's a dumb question. I appreciate your help!
 
If what I posted above is correct coding, then I guess it's not. Any suggestion as to what the problem might be? I'm clueless as to where to start. :(

Thanks again for your help!
 
When passing data between Master and Content pages, I generally set up public properties for everything I want to pass. Then, I can just cast my Master or Content page to it's relevant type and access the properties directly.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
As I mentioned before I'm still in the early learning process, so could you show me an example of what you mean? Where would I set up the public properties? Since I'm passing to the content page would the properties I set up be for "lblType" ?

Thank you!
 
Code:
<%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>

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

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
Code:
Partial Class site
    Inherits System.Web.UI.MasterPage

    Public Property MyText()
        Get
            Return Label1.Text
        End Get
        Set(ByVal value)
            Label1.Text = value
        End Set
    End Property
End Class

Code:
<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default60.aspx.vb" Inherits="Default60" title="Untitled Page" %>
<%@ MasterType TypeName="site" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</asp:Content>
Code:
Partial Class Default60
    Inherits System.Web.UI.Page

    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        txtTest.Text = CType(Page.Master, site).MyText
    End Sub
End Class
Does that make sense or do you need an explanation?


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Thank you for the example, however, I still cannot get it to return anything to my content page. Does it matter that lblType.text on the Master page is set programmatically or that it is a hidden label?
 
Thanks for taking pity on the ignorant. :D

Here is my example:

Code:
<%@ Master Language="VB" CodeFile="sample.master.vb" Inherits="sample" %>

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

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblType" runat="server" />
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

Code:
Imports System.Data.OleDb
Imports System.Data
Partial Class sample
    Inherits System.Web.UI.MasterPage

    Public Property MyText()
        Get
            Return lblType.Text
        End Get
        Set(ByVal value)
            lblType.Text = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If lblType.Text Is Nothing Then
            lblType.Text = "Admin"
        Else
            lblType.Text = "User"
        End If
    End Sub
End Class

Code:
<%@ Page Language="VB" MasterPageFile="~/sample.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</asp:Content>

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtTest.Text = CType(Page.Master, sample).MyText
    End Sub
End Class
 
I just realized in my sample that I set up, it is placing a label on the default.aspx page before the text box. What on earth did I do?

Melinda
 
Okay, it's too early. I see what I did. I just misunderstood my own page. As you can see from my sample, my textbox is not populating the value from my label. This is what is happening in my application also. I stepped through with the debugger and I find that when the system is setting the MyText() property, it is doing so before the page loads so at the time lblType is null. What can I do to fix that?

Thanks again for all your help!
 
I figured it out! I moved the code from the page_load event from the master page and put it in the page_init event and it worked perfectly! Thank you so much for your help! Have a great Friday!

Melinda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top