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

Hi

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>";
        Response.Write(script);

Reena Jain replied to abinav shankar on 01-Feb-12 04:35 AM
Hi,
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
hi,

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">

  .modalBackground

  {

  background-color#CCCCFF;

  filteralpha(opacity=40);

  opacity0.5;

  }

  .ModalWindow

  {

  bordersolid1px#c0c0c0;

  background-color#99ccff;

  padding0px10px10px10px;

  positionabsolute;

  top-1000px;

  }

</style>

ModapPopUp Code

------------------------

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

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

ID="ModalPopupExtender2" runat="server">

</cc1:ModalPopupExtender>

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

<%--Button popup--%>

<center>

<table runat="server">

<tr>

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

  valign="top">

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

  Deletion</div>

  </td>

</tr>

<tr>

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

  <br />

  The Record has been deleted!

  </td>

</tr>

</table>

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

</center>

</asp:Panel>

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