【问题标题】:Calling JS function from C# Page load从 C# 页面加载调用 JS 函数
【发布时间】:2017-01-06 09:49:04
【问题描述】:

我正在尝试使用 C# 从后面的代码中调用 js 函数,但它不起作用。下面是我的代码

Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "Sys.Application.add_load( function(){ setText(" TExt ")});", true);

我做错了什么

【问题讨论】:

    标签: javascript c# jquery webforms


    【解决方案1】:

    你可以试试这个。

    ScriptManager.RegisterClientScriptBlock(Page,this.GetType(), Guid.NewGuid().ToString(), "Sys.Application.add_load( function(){ Mret.FormInputControl._setText(" + string.Format(CultureInfo.InvariantCulture, "'{0}','{1}'", this.ID, TextHeader) + ")});", true);
    

    以下是从后面的代码调用 javascript 函数的不同方式

    解决方案 1:

    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Hello');", true);
    

    解决方案 2:

    ScriptManager.RegisterStartupScript(Page,this.GetType(), "alert", "alert('Hello');", true);
    

    解决方案 3:

    ScriptManager.RegisterClientScriptBlock(Page,this.GetType(), "alert", "alert('Hello');", true);
    

    【讨论】:

    • 你可以试试这个,因为它适用于更新面板或没有更新面板
    • 我试过了。想知道我的脚本是否在 Scriptmanager 中
    • 你能试试这个工作吗 ScriptManager.RegisterStartupScript(Page,this.GetType(), Guid.NewGuid().ToString(), "alert(123);", true);
    • 我已经尽力了。所有人都在工作。您是否尝试过警报?
    • 或者你可以检查浏览器开发者控制台是否有错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多