Run the javascript function after the ASP.NET page has finished loading.

I need to run a javascript function from ASP.NET code behind AFTER the page is finished.

I have used this code so far, but it returns "undefined" because the hidden field is not populated with value when the javascript function is run.

What should I do? Thanx in advance.

Aspx:

<asp:HiddenField runat="server" ID="ColorHiddenField" ClientIDMode="Static" Value="0" /> 

JavaScript:

  function HandleColors() { alert($('#<%= ColorHiddenField.ClientID %>').val()); } 

Code for:

  ColorHiddenField.Value = item.Color; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "HandleColors();", true); 
+8
javascript jquery c #
source share
3 answers

try the code below, it uses jQuery document.ready to run the script after the page loads:

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { HandleColors(); });", true); 
+20
source share

use RegisterStartupScript instead of RegisterClientScriptBlock, e.g.

 ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "HandleColors();", true); 
+2
source share

try with a ready-made jquery document.

 $( document ).ready(function() { alert($('#<%= ColorHiddenField.ClientID %>').val()); }); 
+1
source share

All Articles