Payam Sh
Payam Sh

Reputation: 601

Run a javascript function after ASP.NET page load is completed

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

I've used this code so far but it returns "undefined" because the hidden field is not filled with the value when javascript function is fired.

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 Behind:

  ColorHiddenField.Value = item.Color;
  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "HandleColors();", true);

Upvotes: 16

Views: 43130

Answers (3)

TouchStarDev
TouchStarDev

Reputation: 441

use RegisterStartupScript instead of RegisterClientScriptBlock like

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "HandleColors();", true);

Upvotes: 4

maruthu chandrasekaran
maruthu chandrasekaran

Reputation: 168

try with jquery document ready.

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

Upvotes: 2

Mohamad Shiralizadeh
Mohamad Shiralizadeh

Reputation: 8765

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

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

Upvotes: 32

Related Questions