PDA

View Full Version : [CLOSED] Adding Custom Events



conman
Oct 09, 2008, 7:51 AM
Hiya,

I am wondering what the best way to add custom events to controls.

An example is the "mousdown" and "mouseup" events on a button. I can do this by writing javascript code such as




protected void Page_Load(object sender, EventArgs e)
{
string fn = "Ext.get('mybutton').on('mousedown',function(){aler t('mousedown')});";
this.ScriptManager1.RegisterOnReadyScript(fn);

}


What I was trying to use initially was manipulate the button control directly through code snippets (which I could not get to work) such as




this.mybutton.AddEvents("mousedown");
this.mybutton.AddListener("mousedown", fn);



Firstly, is it possible to achieve the above through code on the server side, or through the Listener "Add" tag in the control itself?

But my main question is, which way would you recommend we achieve the required results?

thanks,
Conor

geoffrey.mcgill
Oct 09, 2008, 8:18 AM
Hi Conor,

I'm writting a sample for you, but I just have one quick question... I'm assuming "mybutton" is an <ext:Button> control? or is it an <asp:Button> control?

conman
Oct 09, 2008, 8:27 AM
You assume correctly :-)

Sorry, I didnt think of mentioningthat. I also didnt know you could add ext listeners to asp:button controls...

thanks.

geoffrey.mcgill
Oct 09, 2008, 8:45 AM
Hi Conor,

Thanks for the clarification.


But my main question is, which way would you recommend we achieve the required results?

Well... you've got options. I added a new example to the explorer which demonstrates several techniques for adding a Listener to a Button. The example uses the "click" listener, but the same code would apply for any Listener you want to add.

See http://examples1.ext.net/#/Listeners/Basic/Add_Listener/

Hope this helps.

geoffrey.mcgill
Oct 09, 2008, 8:46 AM
I just noticed that you are adding the "mousedown" and "mouseup" which are not directly supported listeners on <ext:Button>.

Give me a few minutes to come up with another example.

michaeld
Sep 04, 2009, 8:50 PM
I'm not a premium support member as yet, but I had a similar challenge I thought I'd share here since it's related to this post. I matched the code found in your examples explorer under Listeners Basic to add additional listeners that were not discovered when using Intellisense with the Listeners property.

I found when I tried adding listeners to panels, labels and a few other coolite controls, I could not add mouseover or mouseout events as follows:





var lbl = new Coolite.Ext.Web.lbl("Test");
lbl.ID = String.Format( "ctlM00", Month, Day );
pnl.BodyControls.Add( lbl );
lbl.AddEvents( "'mouseover'" ); // note: i had to add apostrophies to get the client-side to compile
lbl.AddEvents( "'mouseout'" );
lbl.On( "mouseover", "function(p) { alert('in') }" );
lbl.On( "mouseout", "function(p) { alert('out') }" );
I tried with and without the AddEvent calls. I tried adding a listener to the 'click' event this way and that didn't do the trick either. I tried adding the listener instead to the panel, and still nada. I may have done something blatently wrong, but I believe the above is essentially the same code at the beginning of this posting.

So, to work-around this problem, the other option from this posting overcame:





string fn = "Ext.get('" + lbl.ClientID + "').on('mouseover',function(p) { alert('in') });";
this.ScriptManager1.RegisterOnReadyScript( fn );




Though, I suspect both should have worked the same.

Another side-note, you might want to assert when clientID is null when you call addListener because the String.Format call in AddListner puts ClientID in {0} and it's not created automatically if dynamically created.