This tutorial was created with Visual Studio.NET 2008. 2005 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 is not met.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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:
| <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</form> |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</form> |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</form> |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Now we can add the controls to our ContentTemplate:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
Time now: <%= DateTime.Now.ToString("T") %>
<br /><br />
Please type Update in the textbox, and hit the button:<br />
<asp:TextBox ID="txtUpdate" runat="server" /><br />
<asp:Button ID="butUpdate" runat="server" Text="Update"
onclick="butUpdate_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form> |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Notice we have the button onclick attribute set to call a method. The method will look something like this:
Protected Sub butUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUpdate.Click
If txtUpdate.Text = "Update" Then
UpdatePanel1.Update()
End If
End Sub |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
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:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub butUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUpdate.Click
If txtUpdate.Text = "Update" Then
UpdatePanel1.Update()
End If
End Sub
End Class |
To download this project, enter your email address and a link will be emailed to you.