简体   繁体   中英

ASP.Net - Changing text of Label on Content Page from Master Page

How do i change a simple Label which is in the Content Page.

From the master page?

All the help seems to only explain how to access controls in master page from content pages. which is not what I need.

I've tried everything and It's quite frustrating:/

I have no code to show because I'm not exactly sure of what I tried and what code even remotely works. Anyone can help me?

I'm looking for a simple way to change the text.. Something like...

//On master Page:
contentPageName.Label1.Text = "test";

Ok, so your child page will have this:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

Keep in mind, that often you might have two content sections - when you created the master + child page - it comes down to the developer habits as to which content area you shoved/created the child page under.

So, in above, we have "MainContent"

So, to modify say a text box from the master page (menu bar code or whatever), then you can do this:

    Dim MyContent As ContentPlaceHolder
    MyContent = Me.FindControl("MainContent")

    Dim MyTextBox As TextBox = MyContent.FindControl("TextBox1")

    MyTextBox.Text = "Changed from main master page"

So, in above, we are running the above code from/inside of the master.site code behind.

We first grab reference to the content page (NOTE the ContentPlaceHolderID - "MainContent" - that's what we grab.

Then we grab a reference to the given control we want to get our hands on.

Then we simply do whatever we want to that child control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM