Adding "onunload" JavaScript event handler to body in code-behind

Discussion in 'ASP .Net' started by Laurent Bugnion, Jan 21, 2006.

  1. Hi,

    I am trying to programatically add an "onunload" event handler to the
    "body" tag of an ASPX page. I am doing this from a Custom Control
    located on this page.

    To do this, I added "runat=server" to the body tag, which provides me
    access to it in the code-behind through a HtmlControl. I can then
    manipulate the Attributes collection, which is what I needed. So far so
    good.

    However, I found out that "runat=server" generates a compilation error
    (CS1012: Too many characters in character literal) if the "body" tag
    already has an onunload event handler (in the ASPX code). This is a
    problem, because I wish that the user of my custom control be free to
    specify event handlers in the body tag, and my control should simply add
    its own.

    So the question is: Is there a way to use "runat=server" on a body tag
    with an onunload event handler?

    I am using the framework 1.1.

    Unfortunately, there are no "RegisterEndScript" methods (similar to
    "RegisterStartupScript") in the Page class ;-)

    Thanks,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Jan 21, 2006
    #1
    1. Advertising

  2. You can use the window object. Example:

    string scrp = @"<script>
    window.onunload =test;
    function test()
    {
    alert('yo');
    }
    </script>

    Page.RegisterClientScriptBlock("unload",scr);

    --Peter
    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Laurent Bugnion" wrote:

    > Hi,
    >
    > I am trying to programatically add an "onunload" event handler to the
    > "body" tag of an ASPX page. I am doing this from a Custom Control
    > located on this page.
    >
    > To do this, I added "runat=server" to the body tag, which provides me
    > access to it in the code-behind through a HtmlControl. I can then
    > manipulate the Attributes collection, which is what I needed. So far so
    > good.
    >
    > However, I found out that "runat=server" generates a compilation error
    > (CS1012: Too many characters in character literal) if the "body" tag
    > already has an onunload event handler (in the ASPX code). This is a
    > problem, because I wish that the user of my custom control be free to
    > specify event handlers in the body tag, and my control should simply add
    > its own.
    >
    > So the question is: Is there a way to use "runat=server" on a body tag
    > with an onunload event handler?
    >
    > I am using the framework 1.1.
    >
    > Unfortunately, there are no "RegisterEndScript" methods (similar to
    > "RegisterStartupScript") in the Page class ;-)
    >
    > Thanks,
    > Laurent
    > --
    > Laurent Bugnion, GalaSoft
    > Software engineering: http://www.galasoft-LB.ch
    > Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    > Support children in Calcutta: http://www.calcutta-espoir.ch
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 21, 2006
    #2
    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. David Orr
    Replies:
    0
    Views:
    2,375
    David Orr
    Feb 17, 2004
  2. Replies:
    1
    Views:
    682
    Damien
    Feb 22, 2007
  3. Perttu Pulkkinen

    admin logout & body onunload event

    Perttu Pulkkinen, May 17, 2004, in forum: Javascript
    Replies:
    1
    Views:
    138
    Randy Webb
    May 17, 2004
  4. Roy Wang
    Replies:
    3
    Views:
    82
  5. Replies:
    5
    Views:
    1,306
    dhtml
    Jan 22, 2009
Loading...

Share This Page