Adding ajax/som event handler programmatically

Discussion in 'ASP .Net' started by news.rz.uni-karlsruhe.de, Jan 27, 2008.

  1. I wrote an event handler with
    Sys.UI.DomEvent.addHandler($get("MyButton"),"click",myfunc);

    function myfunc(event)

    {

    alert("Hi!");

    }

    1.) If I write this code as a javascript block after the page under the
    closing </html> tag everything works fine.

    2.) If I write this code as a javascript block between the <head> tags I get
    an null pointer error from $get("MyButton").

    3.) I get the same error if I use the ScriptManager.Scripts.Add(Reference),
    obviously because the button "MyButton" doesn't exist yet.

    So how can I add my event handler programmatically to achive the same
    effect as in 1.)?



    Regards

    R4DIUM
    news.rz.uni-karlsruhe.de, Jan 27, 2008
    #1
    1. Advertising

  2. Howdy,

    Leave the function declaration as it is, and paste this to Page_Load, or
    Page_PreRender event handler:

    string script = String.Format(
    "Sys.UI.DomEvent.addHandler($get('{0}'),'click',myfunc);",
    btn.ClientID);

    ClientScript.RegisterStartupScript(this.GetType(), "whatever",
    script, true);

    Regards
    --
    Milosz


    "news.rz.uni-karlsruhe.de" wrote:

    > I wrote an event handler with
    > Sys.UI.DomEvent.addHandler($get("MyButton"),"click",myfunc);
    >
    > function myfunc(event)
    >
    > {
    >
    > alert("Hi!");
    >
    > }
    >
    > 1.) If I write this code as a javascript block after the page under the
    > closing </html> tag everything works fine.
    >
    > 2.) If I write this code as a javascript block between the <head> tags I get
    > an null pointer error from $get("MyButton").
    >
    > 3.) I get the same error if I use the ScriptManager.Scripts.Add(Reference),
    > obviously because the button "MyButton" doesn't exist yet.
    >
    > So how can I add my event handler programmatically to achive the same
    > effect as in 1.)?
    >
    >
    >
    > Regards
    >
    > R4DIUM
    >
    >
    >
    >
    Milosz Skalecki [MCAD], Jan 27, 2008
    #2
    1. Advertising

  3. news.rz.uni-karlsruhe.de

    Axel Gallus Guest

    Thx - where exactly do I register the js-file which contains my "myfunc" ?

    Regards

    R4DIUM


    "Milosz Skalecki [MCAD]" <> schrieb im Newsbeitrag
    news:D...
    > Howdy,
    >
    > Leave the function declaration as it is, and paste this to Page_Load, or
    > Page_PreRender event handler:
    >
    > string script = String.Format(
    > "Sys.UI.DomEvent.addHandler($get('{0}'),'click',myfunc);",
    > btn.ClientID);
    >
    > ClientScript.RegisterStartupScript(this.GetType(), "whatever",
    > script, true);
    >
    > Regards
    > --
    > Milosz
    >
    >
    > "news.rz.uni-karlsruhe.de" wrote:
    >
    >> I wrote an event handler with
    >> Sys.UI.DomEvent.addHandler($get("MyButton"),"click",myfunc);
    >>
    >> function myfunc(event)
    >>
    >> {
    >>
    >> alert("Hi!");
    >>
    >> }
    >>
    >> 1.) If I write this code as a javascript block after the page under the
    >> closing </html> tag everything works fine.
    >>
    >> 2.) If I write this code as a javascript block between the <head> tags I
    >> get
    >> an null pointer error from $get("MyButton").
    >>
    >> 3.) I get the same error if I use the
    >> ScriptManager.Scripts.Add(Reference),
    >> obviously because the button "MyButton" doesn't exist yet.
    >>
    >> So how can I add my event handler programmatically to achive the same
    >> effect as in 1.)?
    >>
    >>
    >>
    >> Regards
    >>
    >> R4DIUM
    >>
    >>
    >>
    >>
    Axel Gallus, Jan 27, 2008
    #3
  4. news.rz.uni-karlsruhe.de

    Axel Gallus Guest

    Is there a possibility to have the
    Sys.UI.DomEvent.addHandler($get('ButtonID'),'click',myfunc) in a file as
    well?

    Regards

    RADIUM

    "Axel Gallus" <-karlsruhe.de> schrieb im Newsbeitrag
    news:fnhtte$u19$-karlsruhe.de...
    > Thx - where exactly do I register the js-file which contains my "myfunc" ?
    >
    > Regards
    >
    > R4DIUM
    >
    >
    > "Milosz Skalecki [MCAD]" <> schrieb im
    > Newsbeitrag news:D...
    >> Howdy,
    >>
    >> Leave the function declaration as it is, and paste this to Page_Load, or
    >> Page_PreRender event handler:
    >>
    >> string script = String.Format(
    >> "Sys.UI.DomEvent.addHandler($get('{0}'),'click',myfunc);",
    >> btn.ClientID);
    >>
    >> ClientScript.RegisterStartupScript(this.GetType(), "whatever",
    >> script, true);
    >>
    >> Regards
    >> --
    >> Milosz
    >>
    >>
    >> "news.rz.uni-karlsruhe.de" wrote:
    >>
    >>> I wrote an event handler with
    >>> Sys.UI.DomEvent.addHandler($get("MyButton"),"click",myfunc);
    >>>
    >>> function myfunc(event)
    >>>
    >>> {
    >>>
    >>> alert("Hi!");
    >>>
    >>> }
    >>>
    >>> 1.) If I write this code as a javascript block after the page under the
    >>> closing </html> tag everything works fine.
    >>>
    >>> 2.) If I write this code as a javascript block between the <head> tags I
    >>> get
    >>> an null pointer error from $get("MyButton").
    >>>
    >>> 3.) I get the same error if I use the
    >>> ScriptManager.Scripts.Add(Reference),
    >>> obviously because the button "MyButton" doesn't exist yet.
    >>>
    >>> So how can I add my event handler programmatically to achive the same
    >>> effect as in 1.)?
    >>>
    >>>
    >>>
    >>> Regards
    >>>
    >>> R4DIUM
    >>>
    >>>
    >>>
    >>>

    >
    >
    Axel Gallus, Jan 27, 2008
    #4
    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. Ken Philips
    Replies:
    1
    Views:
    474
    Dave Monroe
    Jul 20, 2004
  2. Odd Einar
    Replies:
    1
    Views:
    405
    Inger Helene Falch-Jacobsen
    Oct 23, 2004
  3. wooks
    Replies:
    1
    Views:
    496
  4. tgtt
    Replies:
    2
    Views:
    435
    C. M. Sperberg-McQueen
    Jan 28, 2004
  5. Replies:
    1
    Views:
    695
    Damien
    Feb 22, 2007
Loading...

Share This Page