C# .NET - To display alert message - Asked By abinav shankar on 01-Feb-12 04:32 AM


I want display alert message and i used the following code but and i am ending up with script error pl tell me where i went wrong

string strMsg = "Cannot raise for more than one leave request in a day";
string script = "<script language=\"javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";

Reena Jain replied to abinav shankar on 01-Feb-12 04:35 AM
There are various ways. It all depends on what and where you want to do this.

Simplest of them all is to write the Script into the Response Object.
Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert("Reena Jain")</SCRIPT>");

Second, you can use the OnClientClick method of the server controls to execute an alert on the client side:
<asp:Button ID="ButtonTestMe" runat="server" Text="Click Me" OnClientClick = "javascript:alert('you clicked me');"/>

And third, you can use the RegisterClientScriptBlock method to write scripts.
string script = @"<script type="text/javascript">alert('eggheadcafe.com);</script>";
page.ClientScript.RegisterClientScriptBlock(typeof("string"), "testalert", script);

Hope this helps.
kalpana aparnathi replied to abinav shankar on 01-Feb-12 04:39 AM

Try this way:
string strMsg = "Cannot raise for more than one leave request in a day";
Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + strMsg + " .');</script>"); 

dipa ahuja replied to abinav shankar on 01-Feb-12 04:40 AM

Using JavaScript simple alert box :

Page.RegisterClientScriptBlock("""<script>alert('Book Added');</script>");

Response.Write("<script>alert('Book Added')</script>");

Or you can display your own window by

using the modalpopupExtender:

CSS Effect


<style type="text/css">
















ModapPopUp Code


<cc1:ModalPopupExtender TargetControlID="Button2" BackgroundCssClass="modalBackground"

CancelControlID="btnMsg" PopupControlID="Panel1" PopupDragHandleControlID="btnMsg"

ID="ModalPopupExtender2" runat="server">


<asp:Panel ID="Panel1" runat="server" CssClass="ModalWindow">

<%--Button popup--%>


<table runat="server">


  <td bgcolor="#cccccc" style="border: 1px; border-color: Black;" height="20px" align="left"


  <div style="border-bottom-width: 1px; border: 1px solid">





  <td align="center" bgcolor="#99ccff">

  <br />

  The Record has been deleted!




<asp:Button ID="btnMsg" runat="server" Text="OK" /><br />



<asp:Button ID="Button2" runat="server" Text="OK" /><br />