This tutorial was created with Visual Studio.NET 2010. 2008 can be used, but Microsoft’s ASP.NET AJAX Extensions, which can be downloaded at this link, must be installed.
Using AJAX in ASP.NET, we are able to programmatically force an UpdatePanel to Update – we do not have to rely solely on triggers. Similarly, we are able to stop an UpdatePanel from updating if certain criteria are not met.

In this tutorial, we will be looking at how we can programmatically make an UpdatePanel update when certain criteria are met. We will be using an UpdatePanel to display the current time, and we’ll also have a textbox and a button, which we’ll use to require the user to enter a string before the UpdatePanel is updated.

The first thing we will do is build our ASPX page. First, we will add the ScriptManager tag, if it has not already been added for us:

The ScriptManager will handle all the AJAX calls for us; ASP.NET makes it really easy for us to implement and use AJAX. We also need to add the UpdatePanel:

Because we will be programmatically updating the UpdatePanel, we need to set two attributes – UpdateMode and ChildrenAsTriggers. We do not want the UpdatePanel to Update all the time (hijack all PostBacks), so we set this to Conditional. We also do not want to specify the child controls of the UpdatePanel as triggers, causing it to update, also. So our UpdatePanel will look like this:

Now we can add the controls to our ContentTemplate:

Notice we have the button onclick attribute set to call a method. The method will look something like this:

If the user followed the instructions, the UpdatePanel will refresh; but if the user made a mistake, the UpdatePanel will not update.

Now hit f5 to debug your application and run it. You will see how you can type update in the box and it will display the current time.

Download Source Files