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 is not met.

In this tutorial, we will be looking at how we can programmatically make an UpdatePanel update when certain criteria is 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.

The entire code-behind looks like this:

Download Source Files