最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Call Javascript function from code-behind in C#.NET - Stack Overflow

programmeradmin3浏览0评论

I am trying to call a javascript simple alert function when I catch an exception in my C# code as follows:

inside my function:

try
{
    //something!
}
catch (Exception exc)
{
    ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", 
     "<script type='text/javascript'>alert('Error !!!');return false;</script>");
}

Is there another way I can do this, because this doesn't show any alert boxes or anything??

I am trying to call a javascript simple alert function when I catch an exception in my C# code as follows:

inside my function:

try
{
    //something!
}
catch (Exception exc)
{
    ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", 
     "<script type='text/javascript'>alert('Error !!!');return false;</script>");
}

Is there another way I can do this, because this doesn't show any alert boxes or anything??

Share Improve this question edited Jul 16, 2010 at 13:27 djdd87 68.5k29 gold badges159 silver badges197 bronze badges asked Jul 16, 2010 at 13:23 Saher AhwalSaher Ahwal 9,23733 gold badges90 silver badges155 bronze badges 1
  • Is this inside an update panel by chance? – Nick Craver Commented Jul 16, 2010 at 13:26
Add a comment  | 

6 Answers 6

Reset to default 9

It's because you'll get the error along the lines of:

Return statement is outside the function

Just do the following, without the return statement:

ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", 
 "<script type='text/javascript'>alert('Error !!!');</script>");

The above should work unless if it is inside update panel. For ajax postback, you will have to use ScriptManager.RegisterStartupScript(Page, typeof(Page), "SymbolError", "alert('error!!!')", true); instead.

Its the return, the below code works:

    try
    {
        throw new Exception("lol");
    }
    catch (Exception)
    {
        ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Error!!!');</script>", false);
    }

Try this

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SymbolError", "alert('error');", true);

Try to use the following:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "AnUniqueKey", "alert('ERROR');", true);

I use this code in an asp.net project

 public void MsgBox1(string msg, Page refP)
{
    Label lbl = new Label();
    string lb = "window.alert('" + msg + "')";
    ScriptManager.RegisterClientScriptBlock(refP, this.GetType(), "UniqueKey", lb, true);
    refP.Controls.Add(lbl);
}

And when I call it, mostly for debugging

MsgBox1("alert(" + this.dropdownlist.SelectedValue.ToString() +")", this);

I got this from somewhere in the web, but was like a year ago and forgot the real original source

here are some related links:

  • Diffrent Methods to show msg box in Asp.net on server side - Coding Resolved
  • Create a Message Box in ASP.NET using C# - Blog Sathish Sirikonda
发布评论

评论列表(0)

  1. 暂无评论