ASP.NET and AJAX: So Freaking Easy (Part 2: Triggers)
My last post highlighted how easy it is to implement asynchronous postbacks using ASP.NET and AJAX but didn’t go into much depth. Here I’m going to explain a little bit more (at least so far as I’ve learned!) about triggers. You most likely have these on your site already and just don’t know it (or do). Any time someone presses a button or selects something from a dropdown that automatically posts back, that’s a trigger. Typically, this performs a full page post back, reloads your page, and performs the designated code for that trigger. In ASP.NET/AJAX, you can decide whether or not you want to trigger a full page refresh or a partial page refresh simply by designating a trigger.
Between your <asp:updatepanel> tags and outside your <contenttemplate> tags, create a <triggers> tag. Inside this tag, you have the option to create two types of tags: <asp:postbacktrigger controlid=”yourControl” /> and <asp:asyncpostbacktrigger controlid=”yourControl” eventname=”yourEvent” />. If you don’t supply either of these, your update panel automatically assumes that all controls within it are the latter. However, sometimes you need to designate a control to do a full page postback. For instance, file uploading won’t work using AJAX (unless you use some crazy IFrame methods), so you will have to set whichever button you use to submit your file to trigger a full page postback (<triggers><asp:postbacktrigger controlid=”yourFileUploadControl” /></trigger> <contenttemplate>…</contenttemplate>).
If you have any other ASP.NET AJAX tips, please post them here!