DennyDotNet

Awesome ASP.NET C# and other cool coding stuff

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent posts

Recent comments

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

ASP.NET AJAX createDelegate and the JavaScript "this"

If you're doing ASP.NET AJAX and you're creating a lot of JavaScript then you are hopefully aware of the Function.createDelegate method. If you're not using the createDelegate method then read on!

When you're building your classes in ASP.NET AJAX you'll have various function that interact with each other. Sometimes you may even have external methods that you're calling. So when you want to reference a javascript object you would probably pass the object through the parameters list. When it comes to doing a Web Service call you have a callback method for success and fail such as:


var el = this;
Client.GetSomeField(1, 5, onSuccess, onFail);

function onSuccess(r) {
 var el = this;
 alert(r);
}


Everything looks dandy. However when I use "this" inside any of the callback methods I won't get the object I am looking for. Lets say I wanted to pass "this" object to my onSuccess function. How could I do that? Well that's where the createDelegate comes in:


var el = this;
var newSuccess = Function.createDelegate(el, onSuccess);

Client.GetSomeField(1, 5, newSuccess, onFail);

function onSuccess(r) {
 // You now have access to the same object using "this"
 var hw = this.HelloWorld();
 alert(r + hw);
}


Wow well that was easy! The cool thing about this is that you can pass any object with the createDelegate method. So if I wanted to pass a string I could do:


var el = "Hey I came from down the way";
var newSuccess = Function.createDelegate(el, onSuccess);

Client.GetSomeField(1, 5, newSuccess, onFail);

function onSuccess(r) {
 var hw = this; // this = "Hey I came from down the way"
 alert(r + hw);
}


Tada! I just passed my string across.

Well now wait a second, I have another function that takes in a few extra params. It's not within a Web Service method, it's an external function. How do I use createDelegate with that?


var el = this;
var newSuccess = Function.createDelegate(el, onSuccess);

// Call the function
newSuccess(param1, param2, param3);

function onSuccess(p1, p2, p3) {
 // still the same object that we passed
 var hw = this.HelloWorld();
 hw += p1;
 hw += p2;
 hw += p3;
 alert(hw);
}


The createDelegate method takes two parameters. The first one is the object that will be referenced by "this" (inside the function). The second parameter is the function to be executed (where you'll use "this").

Another good trick is to use the createDelegate function with the $addHandler method:


$addHandler(btnSave, 'click', Function.createDelegate(someOtherObj, this.onSaveClicked));


So in conclusion, why use createDelegate? It wraps an existing function and returns a new function which can use the "this" keyword that points to an object of your desire. Why do I need this? In an event handler raised by a DOM element the "this" keyword will refer to the DOM element whereas you may want it to refer to a class or class object, or any other object for that matter.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by SuperGhost on Tuesday, July 24, 2007 8:57 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Programmatically adding an AsyncPostBackTrigger

The AsyncPostBackTrigger is used in ASP.NET AJAX to, as it states, trigger an asynchronous postback. Adding a trigger to an UpdatePanel is fairly straight forward. You can either do it through the UP's Triggers GUI or within the <asp:UpdatePanel> tag under the <Triggers> tag by adding:


<asp:AsyncPostBackTrigger ControlID="someControl" EventName="Click" />

Adding a trigger through any of the previously mentioned methods works as expected. The problem is when you want to set an AsyncPostBackTrigger on a dynamically generated control, like one within a DataList. You cannot do this through the GUI mode because it will not list the control. Instead you have to programmatically add an AsyncPostBackTrigger.

My issue began when I tried to set a trigger on a LinkButton within a DataList. The LinkButton uses the OnCommand event to pass a CommandArgument. In the DataList_OnItemDataBound I added the appropriate code to wire up the AsyncPostBackTrigger:


// Add postback trigger
AsyncPostBackTrigger ap = new AsyncPostBackTrigger();
ap.ControlID = lnkControl.UniqueID;
ap.EventName = "Command";
upFMV.Triggers.Add(ap);

Now it seems rational that the EventName would be "Command" and that this should work the same way as the previous methods. However after running the code I get no UpdatePanel update! I tried a few other event names but to no avail. After posting a question on forums.asp.net I was told to remove the event name. This actually worked but I'm still not sure why.

My code now looks like:


// Add postback trigger
AsyncPostBackTrigger ap = new AsyncPostBackTrigger();
ap.ControlID = lnkControl.UniqueID;
upFMV.Triggers.Add(ap);

It seems to fire for the correct event too. Even though it works if anyone has any insight to this I would greatly appreciate it! And if you're trying to learn how to use the AsyncPostBackTrigger, well now you've got an example.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by Denny on Wednesday, July 18, 2007 4:40 AM
Permalink | Comments (4) | Post RSSRSS comment feed

New Dad, Job and Knowledge

Well it's been a long while since I've blogged and I'm finally getting around to it. There's been a lot of things going on lately! First and foremost I'm going to be a new Dad starting next year around February or March. I'm excited about the news and can't wait to find out if it's a boy or girl. Things will definitely start changing around the house!

Secondly, I started a new job! I'm really excited about working at this new place because they're doing some really cool AJAX stuff. It's a great opportunity for me because I'll get to work with some really bright people and work with the latest technologies too. I recently picked up a book titled: "Professional ASP.NET 2.0 AJAX" by Matt Gibbs and Dan Wahlin (Amazon) to get some further insight on the ASP.NET AJAX Framework. It's an excellent book and there's plenty of tips and knowledge in here even if you're experienced. For example, I finally found out what the benefit of using the ASP.NET AJAX registration methods (for namespaces, classes, etc...). It has to do with performance, amongst other things. I'll blog more about this later! On another note I introduced SubSonic to the team as well. I had some positive feedback and it looks like we could be implementing it across various projects in the future.

I've recently been working with the ASP.NET AJAX Serialization classes, in particular the JSON serializer. There didn't seem to be too many resources while Googling for examples so I poked around and created a few helper functions for myself such as serializing a DataTable or DataReader into a JSON array / object (Yes ASP.NET Futures supports this already). I'll blog about this in the future as well. Anyways, upon creating some helper functions I realized I had something interesting started. I began thinking about how I could bind my array of JSON objects to a pre-defined template, like a Repeater only on the client-side. It seems possible but I haven't researched this thoroughly to see what implications it could have. It may be a side project in the future.

Feedback:
What I'm interested in knowing is how other people have been using ASP.NET AJAX. Are you taking adavntage of Web Services, or are you using PageMethods? Are you relaying your data as XML or JSON? Why? How are you using the data passed to the client. For example are you using it to update one or two specific elements or are you bulking your objects together to complete multiple tasks?

I'm very interested in hearing your responses.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by SuperGhost on Monday, July 16, 2007 5:05 PM
Permalink | Comments (2) | Post RSSRSS comment feed