Click here to Skip to main content
12,253,990 members (65,311 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
hi,

i want to call javascript method from code behind after exception is caught in finally block
i.e.
try
{
 //my code here
}
catch()
{
 //msg
}
finally
{
 
// calling javascript method here

}
Posted 28-May-12 19:38pm
Edited 28-May-12 19:42pm
member6016.1K
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I did a similar thing were I call JavaScript alert box from code behind. perhaps you can also do something like this

check out this and make the changes as per your need to achieve what you want to do.

A Windows Form like MessageBox for ASP.NET Website[^]
  Permalink  
Comments
Prasad_Kulkarni 29-May-12 7:23am
   
Good one +5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

hi,
try like this:

try
{
    //my code here
}
catch (Exception ee)
{
   // throw ee;
}
finally
{
    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "JSFunctionName();", true);
}
  Permalink  
v2
Comments
Rahul Rajat Singh 29-May-12 1:48am
   
This is also a very good answer +5
Member 8861818 29-May-12 2:33am
   
tried but not calling javascript method from finally block
tanweer akhtar 29-May-12 2:49am
   
if you are using Script Manage on your page then the above answer will work other wise you need to add this one
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallJS", "JSFunctionName();", true);
amolpatil2243 29-May-12 8:11am
   
my 5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you can try such as :
finally
{
string script = "<script language="'javascript'">yourFunction();</script>";
 Page.RegisterClientScriptBlock("xxx", script);
}

and the function should be in the head section of page such as:
<script type="text/javascript">
        function yourFunction() {
            alert("Hai");
            return;           
        }
    </script>

for more on this topic refer:http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx[^]
  Permalink  
v5
Comments
Member 8861818 29-May-12 2:34am
   
tried but not calling javascript method from finally block
member60 29-May-12 7:09am
   
k! check my solution again I changed it.It should work.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

try
      {
          //my code here
      }
      catch (Exception ee)
      {
         // throw ee;
      }
      finally
      {
 
          ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJSFunction", "YourFunctionName()", true);
      }


you can write your js function which you can set in a string variable like this


try
      {
          //my code here
      }
      catch (Exception ee)
      {
         // throw ee;
      }
      finally
      {
string jsFunction="alert('Process comlete');"
          ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJSFunction", "jsFunction", true);
      }
  Permalink  
v2
Comments
Member 8861818 29-May-12 2:34am
   
tried but not calling javascript method from finally block
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

try
{
}
catch(exception ex)
{
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Alert", "<script type="Text/javascript">alert(' & ex.message &')</script>", true);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 29 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100