locked
Update Label in Master Page from Content Page won't work RRS feed

  • Question

  • User1769015664 posted

    In the following code, lUser returns current label (line 1) and debugging shows lUser .Text has the new value "New User"

    but the label on master page still displays same old user.

    Label lUser = ((Label)Master.FindControl("lblUser"));  //Old User
    lUser .Text = "New User";
    

    Wednesday, October 7, 2020 9:49 PM

All replies

  • User409696431 posted

    Is this an error in your code, or a typo in your post?

    lUser .Text = "New User";
    
    should be
    
    lUser.Text = "New User";

    Thursday, October 8, 2020 1:39 AM
  • User-939850651 posted

    Hi NJ2,

    According to the relevant code you provided, I am not sure whether you are operating it elsewhere, or not sure where you have executed the currently given code. Therefore, an accurate reproduction of the problem may not be possible.

    I can only guess that the problem is related to postbacks,here is a simple example:

    Master Page code:
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head> <body> <form id="form1" runat="server"> <div> <asp:Label runat="server" ID="lblUser"></asp:Label> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> Master Page code behind: protected void Page_Load(object sender, EventArgs e) { lblUser.Text = "Old User"; }
    Content page:
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <br /><br />
        <asp:Button runat="server" ID="submitBtn" Text="submit" OnClick="submitBtn_Click" />
    </asp:Content>
    
    Content Page code behind:
    
    protected void Page_Load(object sender, EventArgs e)
            {
                Label lbl = (Label)Master.FindControl("lblUser");
                lbl.Text = "New User";
            }
    
            protected void submitBtn_Click(object sender, EventArgs e)
            {
                
            }
    

    When the button in the content page is clicked, the code execution order: Content page Page_Load() --> Master Page Page_Load() --> Content Page submitBtn_Click().

    So it is not only postback to the Content page, but also postback to the Master page. So its result will keep displaying "Old User".

    In this case, you need to perform corresponding operations on the postback in the master page, for example:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    lblUser.Text = "Old User";
                }
            }

    Or store the relevant data in other places, such as Session or Cookie, and then set it on the master page.

    If I misunderstood something, could you provide more details?

    Best regards,

    Xudong Peng

    Thursday, October 8, 2020 2:26 AM
  • User1769015664 posted

    Thew code is fine, mustyped when posted here.

    Thursday, October 8, 2020 4:08 AM
  • User1769015664 posted

    It worked, thanks

    Thursday, October 8, 2020 5:41 AM