完成ASP.NET页面加载后运行javascript函数

时间:2022-10-12 21:49:40

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

我需要在页面完成后从ASP.NET代码运行一个javascript函数。

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.

到目前为止我已经使用过这段代码,但它返回“undefined”,因为当javascript函数被触发时,隐藏字段没有填充值。

What should I do? Thanx in advance.

我该怎么办?提前完成。

ASPX:

ASPX:

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

Javascript:

使用Javascript:

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

Code Behind:

代码背后:

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

3 个解决方案

#1


22  

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

尝试下面的代码,它使用jQuery document.ready在页面加载后运行你的脚本:

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

#2


3  

use RegisterStartupScript instead of RegisterClientScriptBlock like

使用RegisterStartupScript而不是RegisterClientScriptBlock

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

#3


1  

try with jquery document ready.

尝试使用jquery文件准备好。

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

#1


22  

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

尝试下面的代码,它使用jQuery document.ready在页面加载后运行你的脚本:

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

#2


3  

use RegisterStartupScript instead of RegisterClientScriptBlock like

使用RegisterStartupScript而不是RegisterClientScriptBlock

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

#3


1  

try with jquery document ready.

尝试使用jquery文件准备好。

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