calling javascript function from asp.net

Discussion in 'ASP .Net' started by szabelin, Aug 13, 2003.

  1. szabelin

    szabelin Guest

    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!
     
    szabelin, Aug 13, 2003
    #1
    1. Advertising

  2. 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$...
    > 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!
     
    Kevin Spencer, Aug 13, 2003
    #2
    1. Advertising

  3. szabelin

    szabelin Guest

    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-----
    >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$...
    >> 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!

    >
    >
    >.
    >
     
    szabelin, Aug 13, 2003
    #3
  4. szabelin

    szabelin Guest


    >-----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$...
    >> 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-----
    >> >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$...
    >> >> 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!
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    szabelin, Aug 13, 2003
    #4
  5. 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" <> wrote in message
    news:...
    > 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$...
    > > 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-----
    > > >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$...
    > > >> 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!
    > > >
    > > >
    > > >.
    > > >

    >
    >
     
    Kevin Spencer, Aug 14, 2003
    #5
  6. szabelin

    szabelin Guest

    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" <> wrote in message
    >news:uJ$...
    >> > 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!"
    >>
    >>

    >
    >
    >.
    >
     
    szabelin, Aug 15, 2003
    #6
  7. szabelin

    szabelin Guest

    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$...
    >> 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" <> wrote in message
    >> >news:uJ$...
    >> >> > 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!"
    >> >>
    >> >>
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    szabelin, Aug 16, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anatoly
    Replies:
    1
    Views:
    9,008
    =?Utf-8?B?TmlsZXNodw==?=
    Feb 17, 2004
  2. =?Utf-8?B?S0s=?=
    Replies:
    6
    Views:
    31,778
    errorocean
    Mar 22, 2010
  3. Andreas Lagemann
    Replies:
    8
    Views:
    484
    Mike Wahler
    Jan 10, 2005
  4. tiwy
    Replies:
    0
    Views:
    445
  5. Replies:
    2
    Views:
    376
    bruce barker
    Dec 12, 2006
Loading...

Share This Page