Click here to Skip to main content
15,885,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

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

// calling javascript method here

}
Posted
Updated 28-May-12 19:42pm
v3

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[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 29-May-12 7:23am    
Good one +5!
hi,
try like this:

C#
try
{
    //my code here
}
catch (Exception ee)
{
   // throw ee;
}
finally
{
    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "JSFunctionName();", true);
}
 
Share this answer
 
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 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+
you can try such as :
XML
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:
JavaScript
<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[^]
 
Share this answer
 
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.
C#
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


Java
try
      {
          //my code here
      }
      catch (Exception ee)
      {
         // throw ee;
      }
      finally
      {
string jsFunction="alert('Process comlete');"
          ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJSFunction", "jsFunction", true);
      }
 
Share this answer
 
v2
Comments
Member 8861818 29-May-12 2:34am    
tried but not calling javascript method from finally block
C#
try
{
}
catch(exception ex)
{
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Alert", "<script type="Text/javascript">alert(' & ex.message &')</script>", true);
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900