calling javascript function from asp.net

  • Thread starter Thread starter szabelin
  • Start date Start date
S

szabelin

Hello, I am trying to call a function inside the
javascript block from asp.net during the postback (NOT
button's OnClick event handler though). The javascript
function creates new popup window. I register the
javascript code with the RegisterClientScriptBlock.

How how do I call the javascript function inside the block
from within C# asp.net page.

(yes I know popups are bad, please do not reply if you
only want to comment on why this is so)

Thanks!
 
Popups are not bad, as long as they don't have SPAM in them! ;-) Seriously,
popup windows can perform a large variety of useful services, especially in
a server-side app.

Here's the thing. You can't exactly CALL a popup window on the client from
the server, as there is no connection between the 2. What you CAN do is to
use Page.RegisterStartupScript() to place a script in the page so that when
the page reaches the client, the popup window will pop up. Don't just put a
function in; put some code outside of a function that executes
window.open(). Any JavaScript code that is not inside a function block
executes immediately as the browser parses it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 
Thank you for replying Kevin. Now the popup appears for a
fraction of a second and immidiately becomes minimized.
How do I keep it on top of the "caller" IE window? Thanks!

Here is the code:

string PopUpWindowPage = "usermessage.aspx";

string Script = "";
Script += "\n<script language=JavaScript
id='PopupWindow'>\n";
Script += "var confirmWin = null; \n";
Script += "confirmWin = window.open('"+
PopUpWindowPage+"','','width=455,height=435,status');\n";
Script += "</script>";

if(!page.IsClientScriptBlockRegistered("PopupWindow"))
{
page.RegisterClientScriptBlock
("PopupWindow",Script);
}
 
-----Original Message-----
I don't know. It shouldn't behave that way. Do you have some kind of popup
window deterrent device installed on your machine? One other possibility: In
the window.open() method, your "features" string ends with ", status". That
should read ",status=1" or ",status=0".


No this is for corporate intranet - no blockers. I got rid
of status, but I am still having this issue.

BTW, do all of your concatenation in one statement to
save memory.

I agree, good point; I am not big on Javascript so I
copied the code from somebody who's not not big on the
concept of immutable strings in C# :)
And it isn't necessary to initialize your variables prior
to assigning them.

Right, except if few cases (such as switch statement,ect).
 
I'm wondering if there is some JavaScript in the parent page that causes it
to regain focus after losing it...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 
Thank you all very much for replying. body
onload="window.focus()" worked as long as users close the
popup. If they minimize the first popup page and do
another run, then all of the subsequent popups will appear
in minimized state. Moreover, I have to restart the app
for this to go away. All popups are an instance of the
same asp page.

I guess this is just as good as it gets. Also I have other
popup windows which get generated as a result of a button
click inside DataGrid (by adding OnClick attribute) - and
this always works fine.
 
this is how I ended up getting this to work: I register
showWin in RegisterStartupScript if false == IsPostBack
(except I had to take out if(!myWin.closed) part), and
then I call showWin from RegisterClientScriptBlock and it
works every time now. MS should implement this feature for
us - imo developers should not have to touch javascript -
unless you give us a way to compile and step through the
code. You too have a good weekend ;)
 
Back
Top