Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Javascript function from code-behind in C#.NET

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??

like image 897
Saher Ahwal Avatar asked Jan 22 '26 00:01

Saher Ahwal


1 Answers

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>");
like image 158
djdd87 Avatar answered Jan 24 '26 12:01

djdd87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!