Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > calling javascript function from asp.net

Reply
Thread Tools

calling javascript function from asp.net

 
 
szabelin
Guest
Posts: n/a
 
      08-13-2003
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!
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      08-13-2003
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.

"szabelin" <szabelin@szabelin> wrote in message
news:0f8f01c361db$5fe39bb0$(E-Mail Removed)...
> 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!



 
Reply With Quote
 
 
 
 
szabelin
Guest
Posts: n/a
 
      08-13-2003
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("PopupWindo w"))
{
page.RegisterClientScriptBlock
("PopupWindow",Script);
}






>-----Original Message-----
>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.
>
>"szabelin" <szabelin@szabelin> wrote in message
>news:0f8f01c361db$5fe39bb0$(E-Mail Removed)...
>> 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!

>
>
>.
>

 
Reply With Quote
 
szabelin
Guest
Posts: n/a
 
      08-13-2003

>-----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).




>--
>HTH,
>
>Kevin Spencer
>Microsoft MVP
>..Net Developer
>http://www.takempis.com
>Neither a follower nor a lender be.
>
>"szabelin" <szabelin@szabelin> wrote in message
>news:044501c361e2$d53b6ad0$(E-Mail Removed)...
>> 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("PopupWindo w"))
>> {
>> page.RegisterClientScriptBlock
>> ("PopupWindow",Script);
>> }
>>
>>
>>
>>
>>
>>
>> >-----Original Message-----
>> >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.
>> >
>> >"szabelin" <szabelin@szabelin> wrote in message
>> >news:0f8f01c361db$5fe39bb0$(E-Mail Removed)...
>> >> 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!
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      08-14-2003
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.

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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".
>
> BTW, do all of your concatenation in one statement to save memory. And it
> isn't necessary to initialize your variables prior to assigning them.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> http://www.takempis.com
> Neither a follower nor a lender be.
>
> "szabelin" <szabelin@szabelin> wrote in message
> news:044501c361e2$d53b6ad0$(E-Mail Removed)...
> > 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("PopupWindo w"))
> > {
> > page.RegisterClientScriptBlock
> > ("PopupWindow",Script);
> > }
> >
> >
> >
> >
> >
> >
> > >-----Original Message-----
> > >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.
> > >
> > >"szabelin" <szabelin@szabelin> wrote in message
> > >news:0f8f01c361db$5fe39bb0$(E-Mail Removed)...
> > >> 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!
> > >
> > >
> > >.
> > >

>
>



 
Reply With Quote
 
szabelin
Guest
Posts: n/a
 
      08-15-2003
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.



>-----Original Message-----
>Now, why didn't I thiink of that? I guess I was so hung

up on why the window
>lost focus, I wasn't thinking in that direction!
>
>--
>HTH,
>
>Kevin Spencer
>Microsoft MVP
>..Net Developer
>http://www.takempis.com
>Neither a follower nor a lender be.
>
>"Guenther Liebowitz" <(E-Mail Removed)> wrote in message
>news:uJ$(E-Mail Removed)...
>> > Try adding this after window.open
>> > confirmWindow.focus();

>>
>>
>> and after opening the window remember the most

important part.....
>>
>> stick your head out the window and at the top of your

lungs yell,
>> "JAVASCRIPT!"
>>
>>

>
>
>.
>

 
Reply With Quote
 
szabelin
Guest
Posts: n/a
 
      08-16-2003
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



>-----Original Message-----
>Hi
>
>So check if win is open ... close it if so and open a new

one....
>
>var myWin = null; // global pointer to win
>function showWin(sUrl,sFeatures){
>if( myWin != null ) if(!myWin.closed) myWin.close();
>myWin = window.open(sUrl,"myWin",sFeatures);
>myWin.focus();
>}
>
>This should fix your dilemma and you will enjoy the

weekend better
>
>--
>Best Regards
> Vidar Petursson
> ==============================
>Microsoft Internet Client & Controls MVP
> ==============================
>"szabelin" <szabelin@szabelin> wrote in message
>news:054501c3636f$654c1330$(E-Mail Removed)...
>> 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.
>>
>>
>>
>> >-----Original Message-----
>> >Now, why didn't I thiink of that? I guess I was so hung

>> up on why the window
>> >lost focus, I wasn't thinking in that direction!
>> >
>> >--
>> >HTH,
>> >
>> >Kevin Spencer
>> >Microsoft MVP
>> >..Net Developer
>> >http://www.takempis.com
>> >Neither a follower nor a lender be.
>> >
>> >"Guenther Liebowitz" <(E-Mail Removed)> wrote in message
>> >news:uJ$(E-Mail Removed)...
>> >> > Try adding this after window.open
>> >> > confirmWindow.focus();
>> >>
>> >>
>> >> and after opening the window remember the most

>> important part.....
>> >>
>> >> stick your head out the window and at the top of your

>> lungs yell,
>> >> "JAVASCRIPT!"
>> >>
>> >>
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
output text in control location; calling control javascript from page javascript Lisa Calla ASP .Net Building Controls 0 11-09-2004 02:17 AM
Calling Javascript function Krishna ASP .Net 5 07-09-2003 02:23 PM



Advertisments