Updating an UpdatePanel Programmatically in C#


V4 Ajax Tutorials
Server Intellect Cloud Hosting

Updating an UpdatePanel Programmatically in C#

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.

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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 used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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>

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.

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 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!

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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

protected void butUpdate_Click(object sender, EventArgs e)
{
if (txtUpdate.Text == "Update")
UpdatePanel1.Update();
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void butUpdate_Click(object sender, EventArgs e)
{
if (txtUpdate.Text == "Update")
UpdatePanel1.Update();
}
}

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
Veronica said:

Awesome tutorial! Very helpful. Thanks so much!

Posted Sep 19, 2008 2:07 PM
Alee said:

Very Impressive !

Posted Sep 30, 2008 4:17 AM
Web Developer said:

Hello, Thank you for the post, my problem is I have a page with 6 ajax update panels, all load at pageload, I want to have the ability that while this page loads if a user clicks somewhere on the page then all these 6 panels stop loading midway, Reson I want this is because on the top of this page in masterpage I have a navigation, This page had 6 panels so it takes time to load, during this load time if a user clicks some where on the navigation he cant!, he has to wait till the update panels load, I dont want this, I want the user to have the ability to forcefully stop all the update panels and go to some other page if he wishes to go

Posted Nov 9, 2010 3:25 PM
Freelance SEO India said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Dec 29, 2010 11:21 AM
fucking sleeping milf said:

excellent tutorial. thank you

Posted Jan 31, 2011 2:23 PM
www.outlet-louisvuitton.us said:

www.outlet-louisvuitton.us

Posted Aug 6, 2011 3:36 AM
Afrikana Safaris said:

Exactly what I was looking for.

I am also looking for how you can update the updatepanel after every 1minute automatically. Please provide me with anything like this.

Posted Jan 18, 2012 11:27 PM
coach bags said:

coach bags

generally variety within the buying price of $120.00 in order to $450.00, however they perform possess little purses as well as scarfs which are below $100.00.Coach bags, purses and other accessories can definitely add a touch of class to every woman.

coach outlet store

sells Coach products which are moderately priced and yet of good quality.

Posted Feb 12, 2012 8:20 PM
عالم حواء said:

Write more, that’s all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what you’re talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read

Posted May 7, 2012 4:42 AM

Leave a Comment