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

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

Comments

Moshin

Tuesday, September 11, 2007 2:25 PM

Moshin

Thanks for your post.  It helped me a lot! The documentation on this stuff is so rare!

Mohsin

Luis Fleitas

Thursday, September 13, 2007 6:27 AM

Luis Fleitas

What if you set the event name to "Click" ?

This is just a theory, but it might work the second time around because you are not specifying a specific event so the panel updates itself on any event that come from lnkControl.

Gurcan Yılmaz Turkey

Wednesday, November 14, 2007 8:13 PM

Gurcan Yılmaz

I've written the code just as described in the post. But I have to use CommandArgument, in order to run a specific event for each item in the datalist.

Trigger fires the right event but with no CommandEventArgs.

Does anybody have an idea? Thanks in advance...

(The same code works perfect without AJAX by the way. I mean when I use postpack for the button command event)

Gurcan Yılmaz Turkey

Wednesday, November 14, 2007 8:15 PM

Gurcan Yılmaz

not postpack! it's PostBack Smile

Comments are closed