Click here to Skip to main content
11,496,146 members (665 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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 20:38pm
Edited 28-May-12 20:42pm
member6016.1K
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[^]
  Permalink  
Comments
Prasad_Kulkarni at 29-May-12 7:23am
   
Good one +5!
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 at 29-May-12 1:48am
   
This is also a very good answer +5
Member 8861818 at 29-May-12 2:33am
   
tried but not calling javascript method from finally block
tanweer akhtar at 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 at 29-May-12 8:11am
   
my 5+
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 at 29-May-12 2:34am
   
tried but not calling javascript method from finally block
member60 at 29-May-12 7:09am
   
k! check my solution again I changed it.It should work.
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 at 29-May-12 2:34am
   
tried but not calling javascript method from finally block
 
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
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 29 May 2012
Copyright © CodeProject, 1999-2015
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