【问题标题】:Trigger a js function with parameter from code behind使用后面代码中的参数触发 js 函数
【发布时间】:2015-06-23 15:27:32
【问题描述】:

C# 使用后面代码中的参数触发 js 函数。 我有以下代码:

C#:

 ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", String.Format(@"ShowHideMessageBlock('{0}')", @"#successMsg"), true);

js:

function ShowHideMessageBlock(xid) {
    var c = xid;
    console.log(c);

    $(c).fadeIn('slow', function () {
        $(this).delay(5000).fadeOut('slow');
    });
}

当我打开控制台窗口时,我收到以下消息:Uncaught SyntaxError: Unexpected identifier

现在渲染的函数是:

<script type="text/javascript">
//<![CDATA[
ShowHideMessageBlock('#successMsg')Sys.Application.add_init(function() {
    $create(Sys.UI._UpdateProgress, {"associatedUpdatePanelId":null,"displayAfter":500,"dynamicLayout":true}, null, null, $get("updateProgress"));
});
//]]>
</script>

有人可以帮我解决这个问题吗? (它在过去有效)也许我已经改变/破坏了某些东西并且它不再工作了。

【问题讨论】:

  • Sys.Application.add_init 的东西是从哪里来的?另外,我认为ShowHideMessageBlock() 应该以分号结尾。

标签: c# jquery asp.net scriptmanager registerstartupscript


【解决方案1】:

您需要做的就是在String.Format 调用的末尾添加一个分号。

ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", 
    String.Format(@"ShowHideMessageBlock('{0}');", @"#successMsg"), true);

【讨论】:

  • 感谢工作的人。这是一个漫长的一天。我怎么没看到
  • @Babulaas 有时只需要一双新鲜的眼睛;)
  • 非常感谢!我想看看出了什么问题,但这个丢失的分号也在 2020 年修复了这一切 :-)
猜你喜欢
  • 2013-09-25
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
  • 2013-11-13
  • 2015-12-20
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多