Adding ajax/som event handler programmatically

  • Thread starter news.rz.uni-karlsruhe.de
  • Start date

N

news.rz.uni-karlsruhe.de

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
 
Ad

Advertisements

M

Milosz Skalecki [MCAD]

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
 
A

Axel Gallus

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

Regards

R4DIUM
 
Ad

Advertisements

A

Axel Gallus

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

Regards

RADIUM
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top