PC Review


Reply
Thread Tools Rate Thread

Adding ajax/som event handler programmatically

 
 
news.rz.uni-karlsruhe.de
Guest
Posts: n/a
 
      27th Jan 2008
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



 
Reply With Quote
 
 
 
 
Milosz Skalecki [MCAD]
Guest
Posts: n/a
 
      27th Jan 2008
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
>
>
>
>

 
Reply With Quote
 
Axel Gallus
Guest
Posts: n/a
 
      27th Jan 2008
Thx - where exactly do I register the js-file which contains my "myfunc" ?

Regards

R4DIUM


"Milosz Skalecki [MCAD]" <(E-Mail Removed)> schrieb im Newsbeitrag
news4F348B4-8204-4C6D-8964-(E-Mail Removed)...
> 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
>>
>>
>>
>>



 
Reply With Quote
 
Axel Gallus
Guest
Posts: n/a
 
      27th Jan 2008
Is there a possibility to have the
Sys.UI.DomEvent.addHandler($get('ButtonID'),'click',myfunc) in a file as
well?

Regards

RADIUM

"Axel Gallus" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fnhtte$u19$(E-Mail Removed)...
> Thx - where exactly do I register the js-file which contains my "myfunc" ?
>
> Regards
>
> R4DIUM
>
>
> "Milosz Skalecki [MCAD]" <(E-Mail Removed)> schrieb im
> Newsbeitrag news4F348B4-8204-4C6D-8964-(E-Mail Removed)...
>> 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
>>>
>>>
>>>
>>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding AJAX Control Toolkit CollapsiblePanelExtender programmatically Jay Microsoft ASP .NET 0 30th Nov 2009 11:50 PM
How can I programmatically set the handler for a random event andhandler acw Microsoft ASP .NET 0 15th Feb 2009 10:03 PM
adding a event handler in MFC application for c# event Mayur Gadhave Microsoft VC .NET 2 30th Aug 2006 08:14 AM
adding a event handler in MFC application for c# event Mayur Gadhave Microsoft C# .NET 0 30th Aug 2006 07:38 AM
Adding an event handler in a sub Just Me Microsoft VB .NET 6 3rd Sep 2004 09:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:14 AM.