Creating JavaScript alerts in ASP.NET with UpdatePanel

Update Panel

Enables sections of a page to be partially rendered without a postback.

creating an alert when you are using an UpdatePanel is slightly different, but simpler than you might think - you just need to make sure the System.Type Is the? UpdatePanel type, and use the ScriptManager rather than the ClientScript like so

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdatePanel.aspx.cs" Inherits="UpdatePanel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
  <title>Untitled Page</title>
  <form id="form1" runat="server">
      <asp:ScriptManager ID="scriptMangerTest" runat="Server">
      <asp:UpdatePanel ID="updateTest" runat="Server">
                  <table cellpadding="0" cellspacing="0" width="60%">
                          <td align="right">
                              <asp:Label ID="lblName" runat="Server" Text="Enter E-mail Id">
                              <asp:TextBox ID="txtEmail" runat="Server"></asp:TextBox>
                          <td colspan="2" align="left">
                              <asp:Button ID="btnCheck" runat="Server" Text="Validate" OnClick="btnCheck_Click" />
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    protected void btnCheck_Click(object sender, EventArgs e)
        if (!CheckEmail(txtEmail.Text))

            Guid gMessage = Guid.NewGuid();
            string sMessage = "alert('Invalid E-mail Id ');";

            ScriptManager.RegisterStartupScript(updateTest, updateTest.GetType(), gMessage.ToString(), sMessage, true);


    private bool CheckEmail(string EmailAddress)
        string strPattern = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";

        if (System.Text.RegularExpressions.Regex.IsMatch(EmailAddress, strPattern))

        { return true; }

        return false;

Next Post Previous Post
No Comment
Add Comment
comment url