views:

73

answers:

3

Hi,

I am trying to call a JavaScript function from my ascx control code behind in the catch block. I have tried the below two ways but they don't seem to work.

  1. Page.ClientScript.RegisterClientScriptBlock(typeof(string), "script", "test();", true);

  2. ScriptManager.RegisterStartupScript(Page, GetType(), "err_msg", "alert('error');", true);

The function is called if I place the code Under "PageLoad" but doesn't get called when placed in catch block.Should I do any different to call a JavaScript function from catch block. Please suggest.

Thanks

A: 
jebberwocky
Its not working when I assigning in Catch block.
San
@San i tried the following, it works in my case.
jebberwocky
My scenario is - I have a button on master page which opens up an aspx page inside a popup window.When I click the 'save' button on the popup screen the popup screen is closed after the save action is performed. In case of error I will have to show the error message on a field which is in master page. Try catch block is in the pop up page.
San
@San if you use window.open(..), refer the master window as "window.opener." refer:http://www.w3schools.com/jsref/prop_win_opener.asp. Therefore, you can do something like this: window.opener.displayError(), assuming you have displayError function in your master page
jebberwocky
A: 

What is the catch block for and where is it?

If code in a catch block is executed it usually means that something failed, maybe that failure also is the reason the JS call does not go through.

David Mårtensson
In case of Access denied exception I have to show a message on the top of a page with some css styles applied. So I am trying to call a javascript function which does this.
San
Is this called through postback, ajax or by loading the page from start? Does the script run at the right time (after the page has rendered) or does it try to run on an unfinished page?
David Mårtensson
It is being called on postback. On page load if I call the function it works.even - Page.ClientScript.RegisterStartupScript(typeof(string), "script", "Javascript:alert('test');", true); doesn't throw alert in catch block
San
+1  A: 

Have you tried this?

Page.ClientScript.RegisterStartupScript(typeof(string), "script", "test();", true);

I cant recall off the top of my head if that is equivalent to the ScriptManager option in the question.

Also you need to make sure that the "script key" value you are passing in is unique otherwise asp.net will discard all but the first instance of the registered script with the same key.

Thomas James