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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <asp:ScriptManager ID="scriptMangerTest" runat="Server">
      </asp:ScriptManager>
      <asp:UpdatePanel ID="updateTest" runat="Server">
          <ContentTemplate>
              <div>
                  <table cellpadding="0" cellspacing="0" width="60%">
                      <tr>
                          <td align="right">
                              <asp:Label ID="lblName" runat="Server" Text="Enter E-mail Id">
           
                              </asp:Label>
                          </td>
                          <td>
                              <asp:TextBox ID="txtEmail" runat="Server"></asp:TextBox>
                          </td>
                      </tr>
                      <tr>
                          <td colspan="2" align="left">
                              <asp:Button ID="btnCheck" runat="Server" Text="Validate" OnClick="btnCheck_Click" />
                          </td>
                      </tr>
                  </table>
              </div>
          </ContentTemplate>
      </asp:UpdatePanel>
  </form>
</body>
</html>
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;


    }
}

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم