0

I am using a usercontrol to display label text on the left side of a page and another usercontrol to display the content page, when the text of a field on the content page changes, I need to update the same status on a label on the menu usercontrol on the left.

I have used:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"ChangeLabelText", script, false) 

to change the text on the left control, the javascript associated with this gets executed but the label still shows the old text even when the text on the content page is updated after page refresh. However, it shows the new updated text when the whole page refreshes.

Please suggest an approach for resolving this issue.

MukkuP
  • 93
  • 4
  • 13

1 Answers1

0

If you have two UserControls on a page, and want to update a Label in the second Control from code behind of the first, you'll need to use FindControl.

//find the other control in the parent
Control control = Parent.FindControl("WebUserControl2") as Control;

//find the label in that control
Label label = control.FindControl("Label1") as Label;

//update the label
label.Text = "Updated from another control";

Update

If you want to update with javascript you just have to make sure you send the right ID.

ScriptManager.RegisterStartupScript(Page, GetType(), "changeLabel", "changeLabel('WebUserControl2_Label1', 'Updated with JS')", true);

And the javscript function

<script type="text/javascript">
    function changeLabel(id, text) {
        document.getElementById(id).innerHTML = text;
    }
</script>
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • This will work if the page refreshes, but in my case, the left side UC doesn't refresh and I think this needs to be done thru javascript – MukkuP Mar 18 '17 at 12:42
  • Updates my answer. – VDWWD Mar 18 '17 at 13:55
  • I had already this approach, but the value in the left control remains old, i.e. the javascript gets fired before I get the new text value from the server, I need to fire the javascript after I get the new value, any suggestions ? – MukkuP Mar 18 '17 at 21:18