value of Master page label not getting updated form content page
Asked Answered
S

1

6

I have a content page I am updating the value of asp:Label of Master page from content page . value do get updated but the updated value is not visible. I tried two method using

1). defining a property (on master page) to set and get label value. e.g.

public string setErrorMsg
{        
    get { return lbl1.Text; }
    set { lbl1.Text = value; }
}

2) by finding control (label of master page) from content page and setting its text. e.g.

 Label lblMasterError = this.Page.Master.FindControl("lbl1") as Label;
 lblMasterError.Text="text is updated form content page";

both are updating value if I see it in debug mode but updated label value is not visible on content page.What might be the possible reasons for this behavior?

Singh answered 19/6, 2012 at 15:40 Comment(2)
Which event is this code in? It's possible you're setting it after it has been rendered.Shandy
I am updating master page label value on button click event in content page.Singh
B
1

I don't know why is not finding your label but I've had the same to happen before. this is what works for me:

In the master page cs:

public void SetErrorMsg(string ErrorMsg)
{
    this.lbl1.Text = ErrorMsg;
}

From aspx page code behind (replace myMasterPage name w/ yours):

 ((myMasterPage)Master).SetErrorMsg("Some error text");
Bidarka answered 19/6, 2012 at 21:42 Comment(2)
I solved the issues myself after a lot of search. The issue was I am updating master page label through content page having updated panel in it. There are two solution to this issue. 1)give a partial post back using trigger of updated panel on some event. or (2)If you does not want the page postback placed the master page label in a update panel and use find control property for update panel and call the built in update function of update panel UpdatePanel up_pnl= UpdatePanel)Master.FindControl("UpdatePanel1"); then call after updating the label text up_pnl.update().Singh
Hello Muhammad Current I face same issue. Where you find update panel. Is you call this code in master page or content page. Please put your code here.Nephritic

© 2022 - 2024 — McMap. All rights reserved.