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

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

  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);
    }
     
    szabelin, Aug 13, 2003
    #3
  4. szabelin

    szabelin Guest


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

    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# :)
    to assigning them.

    Right, except if few cases (such as switch statement,ect).
     
    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, 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.
     
    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 ;)
     
    szabelin, Aug 16, 2003
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.