UpdatePanel Control in ASP.Net

This article describes how to add partial page update support to a web page using two Mircosoft Ajax server controls, the ScriptManager Control and the UpdatePanel Control.

Introduction

This article describes how to add partial page update support to a web page by using two Microsoft Ajax server controls the ScriptManager Control and the UpdatePanel Control. Using UpdatePanel control we can refresh only required part of the page instead of whole page.

ScriptManager Control

The ScripManager Control manages the partial page updates for UpdatPanel controls that are on the ASP.NET web page or inside a user control on the web page. This control manages the client script for AJAX-enabled ASP.NET web page and ScripManager control support the feature as partial-page rendering and web-service calls.

ScriptManager

UpdatePanel Control

You can refresh the selected part of the web page by using UpdatePanel control, Ajax updatepanel control contains a two child tags that is ContentTemplate and Triggers. In a ContenTemplate tag we used to place the user controls and the Trigger tag allows you to define certain triggers which will make the panel update its content.

  1. <asp:UpdatePanel ID="updatepnl" runat="server">  
  2. <ContentTemplate>  
All the contents that must be updated asynchronously (only contenttemplate parts are updated and rest of the web page part is untouched) are placed here. It allows us to send request Or post data to server without submitting the whole page so that is called asynchronous.

UpdatePanel

Now  I am going to show you how to check the availability of user name in database let's follow the following steps:

Create DataBase and Table in Sql-Server

  1. Create Database Employee  
  2. use Employee  
  3. create table UserDetails  
  4. (  
  5. UserName nvarchar(max)  
  6. )  
Write the following procedure to insert the values in tables columns :
  1. insert into UserDetails values('Pankaj')  
  2. insert into UserDetails values('Nimit')  
Write the following query to execute the table schema:
  1. select * from UserDetails

TableSchema

Step 1

Open Visual Studio-->Create New Website-->ASP.NET Web Site

CreateNewWebsite

Step 2

Now go the solution explorer on to the right side of the application and do the following steps figure given below.

AddNewItem

Step 3

Add new Web form in the empty web application figure given below.

AddNewForm

Write the following code in a CheckAvailability.aspx page :

Step 4

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckAvailability.aspx.cs" Inherits="CheckAvailability" %>  
  2. <%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" tagPrefix="ajax" %>  
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6. <title></title>  
  7. </head>  
  8. <body>  
  9. <form id="form1" runat="server">  
  10. <asp:ScriptManager ID="scriptmanager1" runat="server">  
  11. </asp:ScriptManager>  
  12. <div>  
  13. <asp:UpdatePanel ID="updatepnl" runat="server">  
  14. <ContentTemplate>  
  15. <table>  
  16. <tr>  
  17. <td>Enter UserName </td>  
  18. <td>:</td>  
  19. <td><asp:TextBox ID="txtun" runat="server" AutoPostBack="true" OnTextChanged="CheckUserNameAvailability"/></td>  
  20. <td>  
  21. <div id="checkun" runat="server"  Visible="false">  
  22. <asp:Image ID="shwimg" runat="server" Width="17px" Height="17px"/>  
  23. <asp:Label ID="lblmsg" runat="server"></asp:Label>  
  24. </div>  
  25. </td>  
  26. </tr>  
  27. <tr>  
  28. <td>Enter Password </td>  
  29. <td>:</td>  
  30. <td><asp:TextBox ID="txpwd" runat="server" TextMode="Password"></asp:TextBox></td>  
  31. </tr>  
  32. <tr>  
  33. <td>Enter Confirm Password </td>  
  34. <td>:</td>  
  35. <td><asp:TextBox ID="txtconpwd" runat="server" TextMode="Password"></asp:TextBox></td>  
  36. </tr>  
  37. <tr>  
  38. <td colspan="3"><asp:Button ID="txtbtn" Text="SUBMIT" runat="server" align="center" /></td>  
  39. </tr>  
  40. </table>  
  41. </ContentTemplate>  
  42. </asp:UpdatePanel>  
  43. </div>  
  44. </form>  
  45. </body>  
  46. </html>  
Now Write the following code in CheckAvailability.aspx.cs :

Step 5

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. public partial class CheckAvailability : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.     }  
  14.     protected void CheckUserNameAvailability(object sender, EventArgs e)  
  15.     {  
  16.         if (!string.IsNullOrEmpty(txtun.Text))  
  17.         {  
  18.             SqlConnection con = new SqlConnection("Data Source=MCNDESKTOP34;Initial Catalog=Employee;User ID=;Password=****");  
  19.             con.Open();  
  20.             SqlCommand cmd = new SqlCommand("select * from UserDetails where UserName=@un", con);  
  21.             cmd.Parameters.AddWithValue("@un", txtun.Text);  
  22.             SqlDataReader dr = cmd.ExecuteReader();  
  23.             if (dr.HasRows)  
  24.             {  
  25.                 checkun.Visible = true;  
  26.                 shwimg.ImageUrl = "Cancel.png";  
  27.                 lblmsg.Text = "UserName Already Exist..!!";  
  28.             }  
  29.             else  
  30.             {  
  31.                 checkun.Visible = true;  
  32.                 shwimg.ImageUrl = "Accepted.png";  
  33.                 lblmsg.Text = "Congratulation created Successfully..!!";  
  34.             }  
  35.         }  
  36.         else  
  37.         {  
  38.             checkun.Visible = false;  
  39.         }  
  40.     }  
  41. }  
Step 6

Debug the application by press (F5) to execute Web form. After debugging the application then the output would be as shown below.

AfterDebug 

Step 7

Enter the User Name to check it is available in database or not, figure given below.

UnExists

Step 8

Enter the User Name to check it is available in database or not, figure given below.

UnNotExist

Summary

In this article you saw by using two Microsoft Ajax server controls the ScriptManager Control and the UpdatePanel Control we can refresh only required part of the page instead of whole page.