简体   繁体   English

从代码beind(C#)catch块调用JavaScript函数

[英]Calling JavaScript function from code beind (c#) catch block

I am trying to call a JavaScript function from my ascx control code behind in the catch block. 我试图从catch块后面的ascx控制代码调用JavaScript函数。 I have tried the below two ways but they don't seem to work. 我尝试了以下两种方法,但它们似乎没有用。

  1. Page.ClientScript.RegisterClientScriptBlock(typeof(string), "script", "test();", true);

  2. ScriptManager.RegisterStartupScript(Page, GetType(), "err_msg", "alert('error');", true);

The function is called if I place the code Under "PageLoad" but doesn't get called when placed in catch block.Should I do any different to call a JavaScript function from catch block. 如果将代码放置在“ PageLoad”下,则会调用该函数,但是将其放置在catch块中时不会调用该函数。我应该做些什么来从catch块中调用JavaScript函数。 Please suggest. 请提出建议。

Thanks 谢谢

Have you tried this? 你有尝试过吗?

Page.ClientScript.RegisterStartupScript(typeof(string), "script", "test();", true);

I cant recall off the top of my head if that is equivalent to the ScriptManager option in the question. 如果这相当于问题中的ScriptManager选项,我就想不起来了。

Also you need to make sure that the "script key" value you are passing in is unique otherwise asp.net will discard all but the first instance of the registered script with the same key. 另外,您还需要确保传递的“脚本密钥”值是唯一的,否则asp.net将丢弃具有相同密钥的已注册脚本的第一个实例以外的所有实例。

Javascript does not like at all special characters and NewLine (\\r\\n) characters. Javascript不喜欢所有特殊字符和NewLine(\\ r \\ n)字符。 Replace all of those and it will work. 替换所有这些,它将起作用。

Example: 例:

string test = msgError.Replace("'", "");
test = Server.HtmlEncode(test).Replace(Environment.NewLine, "<br />");
Page.ClientScript.RegisterStartupScript(
            typeof(string),
            "MyKeyCatch",
            //script,
            "showErrorMessage('"+test+"');",
            true);

might want to try this: .cs 可能想尝试一下:.cs

public String ScriptToRun = "test();";

.aspx 的.aspx

$(document).ready(function() {<%=ScriptToRun %>}); //or you can register event to document mannually

Remember that whatever you done in backend is going to generate HTML, Css& javascript to browser. 请记住,您在后端所做的任何事情都会生成HTML,CSS和JavaScript到浏览器。

update: I tried the following code, it works in my case. 更新:我尝试下面的代码,它适用于我的情况。 could you please provide more detail? 您能否提供更多细节? .cs 的.cs

public String script = "";

protected void Page_Load(object sender, EventArgs e)
{
    throwExcep();
}

private void throwExcep()
{
    try
    {
        throw new NotImplementedException();
    }
    catch (Exception e)
    {
        script = "console.log('exception throws from backend message: ["+e.Message+"]')";
    }
}

.aspx: 的.aspx:

<script>
    $(document).ready(function(){
        <%=script  %>
    });
  </script>

What is the catch block for and where is it? 捕获块是什么?它在哪里?

If code in a catch block is executed it usually means that something failed, maybe that failure also is the reason the JS call does not go through. 如果执行catch块中的代码,则通常意味着某些操作失败,也许该失败也是JS调用未通过的原因。

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

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