Reputation: 10547
I have a JavaScript that relies on a HiddenField
value. This HiddenField
, we will call it needUpdate
, is a boolean that gets set to true when a div
on my page needs to be updated with JavaScript. needUpdate
is located in an UpdatePanel
which is updated through a Timer
. I have tried two different methods and both half work. The first is to place the JavaScript outside the update panel and use my PageRequestManager
instance to run the script like so:
<asp:ScriptManager ID="scriptManager" runat="server" />
<script type="text/javascript">
function DoUpdate(sender, args)
{
if ('<%= needUpdate.Value %>' == 'true') {
// Code here... //
}
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(DoUpdate);
</script>
<asp:UpdatePanel ID="replay" runat="server" Visible="false" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="replayTimer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:HiddenField ID="needUpdate" runat="server" Value="false"/>
<asp:Timer ID="replayTimer" runat="server" Interval="500" OnTick="replayTimer_Tick" />
<!-- More Stuff Here -->
</ContentTemplate>
</asp:UpdatePanel>
<div id="divToUpdate"></div>
Now the above runs the script but the value in needUpdate
is never changed, even though it is changed on the back end. So, I tried the next bit of code where I placed the script in the ContentTemplate
area of my UpdatePanel
<ContentTemplate>
<asp:HiddenField ID="needUpdate" runat="server" Value="false"/>
<asp:Timer ID="replayTimer" runat="server" Interval="500" OnTick="replayTimer_Tick" />
<script type="text/javascript">
function DoUpdate()
{
if ('<%= hf_needUpdate.Value %>' == 'true') {
// Code here... //
}
}
DoUpdate();
</script>
<!-- More Stuff Here -->
</ContentTemplate>
</asp:UpdatePanel>
While this updated the script area the code was never called. Any thoughts on how to get a value changed by an UpdatePanel
to feed a JavaScript function?
Upvotes: 0
Views: 257
Reputation: 9530
You are hardcoding a false result into the JavaScript if statement. What gets into the page for:
'<%= hf_needUpdate.Value %>' == 'true'
??
'false' == 'true'
??
This never changes you need to get the current value of the hidden field.
var needUpdate = document.getElementById("needUpdate").value;
if(needUpdate == "true) {
// Do work
}
Upvotes: 1