Adding ajax/som event handler programmatically

Adding ajax/som event handler programmatically

am 27.01.2008 02:31:56 von Axel Gallus

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 tag everything works fine.

2.) If I write this code as a javascript block between the 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

RE: Adding ajax/som event handler programmatically

am 27.01.2008 03:21:01 von mily242

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 tag everything works fine.
>
> 2.) If I write this code as a javascript block between the 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
>
>
>
>

Re: Adding ajax/som event handler programmatically

am 27.01.2008 13:41:24 von Axel Gallus

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

Regards

R4DIUM


"Milosz Skalecki [MCAD]" schrieb im Newsbeitrag
news:D4F348B4-8204-4C6D-8964-DF87B4B9E6F6@microsoft.com...
> 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 tag everything works fine.
>>
>> 2.) If I write this code as a javascript block between the 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
>>
>>
>>
>>

Re: Adding ajax/som event handler programmatically

am 27.01.2008 14:11:28 von Axel Gallus

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

Regards

RADIUM

"Axel Gallus" schrieb im Newsbeitrag
news:fnhtte$u19$1@news2.rz.uni-karlsruhe.de...
> Thx - where exactly do I register the js-file which contains my "myfunc" ?
>
> Regards
>
> R4DIUM
>
>
> "Milosz Skalecki [MCAD]" schrieb im
> Newsbeitrag news:D4F348B4-8204-4C6D-8964-DF87B4B9E6F6@microsoft.com...
>> 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 tag everything works fine.
>>>
>>> 2.) If I write this code as a javascript block between the 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
>>>
>>>
>>>
>>>
>
>