Denny.NET

I can haz ASP.NET goodness?

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

Disclaimer

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

© Copyright 2008

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:

[code:html]
<asp:AsyncPostBackTrigger ControlID="someControl" EventName="Click" />
[/code]

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:

[code:c#]
// Add postback trigger
AsyncPostBackTrigger ap = new AsyncPostBackTrigger();
ap.ControlID = lnkControl.UniqueID;
ap.EventName = "Command";
upFMV.Triggers.Add(ap);
[/code]

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:

[code:c#]
// Add postback trigger
AsyncPostBackTrigger ap = new AsyncPostBackTrigger();
ap.ControlID = lnkControl.UniqueID;
upFMV.Triggers.Add(ap);
[/code]

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

Currently rated 3.3 by 6 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

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

Comments

Moshin

Tuesday, September 11, 2007 7: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 11: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 tr

Thursday, November 15, 2007 1:13 AM

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 tr

Thursday, November 15, 2007 1:15 AM

Gurcan Yılmaz

not postpack! it's PostBack Smile

Jonx

Monday, June 02, 2008 9:11 AM

Jonx

Despite the help saying it cannot be done, it's working:
www.asp.net/.../...eb_UI_AsyncPostBackTrigger.aspx

thank you Smile

Denny us

Thursday, June 05, 2008 5:39 PM

Denny

Also in that documentation it tells you why it works if you do not have an EventName:

The ControlID property is required, but the EventName property is optional. If the EventName property is not specified, the DefaultEventAttribute of the control is used to determine the default event.

Venkat in

Tuesday, July 08, 2008 12:25 AM

Venkat

Thanks for u r comment
It helps me a lot but a small doubt if both postbacktrigger and AsyncPostBackTrigger Ids are same means what we have to do. now i am under same situation pls post ans

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading