简体   繁体   English

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

[英]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. 我需要在页面完成后从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. Thanx提前。

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);

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

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

使用RegisterStartupScript而不是RegisterClientScriptBlock之类的

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

try with jquery document ready. 尝试使用jquery文件准备好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM