How to Create javascript alerts in ASP.NET using UpdatePanel

Sometimes a simpler task becomes difficult.
Similarly when we think of an alert using javascript... its very simple but if you are using update panel then the scenario changes!!!...
Dont worry!!! its still 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 the following code snippet...


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




Untitled Page


































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;


}
}


Comments

Author: ChandraShekar Thota26 Jan 2009 Member Level: Gold   Points : 2

Good

Chandrashekar Thota(Editor, MVP)



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: