Change text of a Label in site.master from code-behind

127 Views Asked by At

I've got an asp.net webforms project with a Site.Master page that's ref'd by my content pages. The header is: <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="JT1.SiteMaster" %>

....

it includes a navbar with a few buttons and a label on the right like so:

               <li><a runat="server" href="~/About">About</a></li>
                <li><a runat="server" href="~/Contact">Contact</a></li>
                <li><a runat="server" href="~/Login">Log In | Sign Up</a></li>
                <li><p runat="server" class="navbar-text navbar-right">
                    <label runat="server" id="LoggedInUserID" >Logged Off</label>
                    </p></li>

All of the buttons show up and work properly, and the text "Logged Off" appears by default on all pages that use the master. From the Login.aspx.cs, I want to change the text of the "LoggedInUserID" label to something appropriate, like "Welcome, Jim". This code is the best I've been able to come up with:

HtmlControl ctl = Master.FindControl("LoggedInUserID") as HtmlControl;
if (ctl != null) ctl.Attributes["InnerText"] = "Welcome " + firstname;

This actually finds the proper control, and I can see in the debugger that its InnerText is, in fact, "Logged Off". But my assignment to it does NOT work. No exception, no error, nothing. I've perused lots of other similar questions and don't see anything I've not tried. Anyone have any ideas?

1

There are 1 best solutions below

10
Albert D. Kallal On

Well, the child page loads first, AND THEN the parent (master) page loads.

So, your code (if on a child page) would run, but THEN the master page loads, and your setting is lost.

However, lets assume we moved the code (and even a test button) to the master page.

When you click on that button, child page load, master page load, your button code runs, label gets set.

But, what happens now if you click on another menu bar item?

Turns out that AGAIN the master page loads. you can think of this as if you navigated to one page, and then navigated back to that page.

As a result, EVEN if you place a button + code in master page to "change" the Label? Well that will at least work + show the change, since master runs AFTER the child page.

However, that control ONLY going to persist now as long as you "stay" on that one master + child page.

This would be no different then if you had a button on the page. Setting label or text box. Such a page can survive post-backs no problem. (controls have automatic viewstate with runat=server)

However, if we navigate to a new page, and then navigate back (not using back button), then that label setting will be lost (and this is how you have to consider the master page).

So, now that we "fixed" the above setting?

Well, we now have to consider if a new menu bar option is hit and now we navigate to a whole new master/child page.

In this navigation, the master page is re-loaded from start. So, once again, you would have to re-run that code.

Long story short?

You need to move that code into the page-load event of the master page.

I mean, a really "neat-o" kluge when you want to modify/change/update/show/see the main menu bar change?

You can execute (in child page that changed the menu "settings" - note I stated menu settings, since we STILL in theory have to change the mnu bar in master page. So, in this context, we might have changed some setting - but it would have to be session type of deal). Now, how to "see" this updated status/change in the nav bar? You can do this:

response.Redirect("name of current page we on.aspx")

That will of course "force" a menu bar refresh. (kind of like hitting a different menu bar option, and then hitting the one we were on).

But, as noted, it only going to see such forced changes if the master page load event "changes" what it was doing in the first place.

(since with runat=server, you get viewstate for that control).

However, the instant you click on another menu item, you have to "consider" this a whole new brand new navigation to that page anyway.