Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Alert.Show(message) From ASP.NET Code-behind

I am reading this JavaScript: Alert.Show(message) From ASP.NET Code-behind

I am trying to implement the same. So I created a static class like this:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Web; using System.Text; using System.Web.UI;  namespace Registration.DataAccess {     public static class Repository     {         /// <summary>          /// Shows a client-side JavaScript alert in the browser.          /// </summary>          /// <param name="message">The message to appear in the alert.</param>          public static void Show(string message)              {                 // Cleans the message to allow single quotation marks                 string cleanMessage = message.Replace("'", "\'");                 string script = "<script type="text/javascript">alert('" + cleanMessage + "');</script>";                  // Gets the executing web page                 Page page = HttpContext.Current.CurrentHandler as Page;                  // Checks if the handler is a Page and that the script isn't allready on the Page                 if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))                 {                   page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);                 }              }      } } 

On this line:

string script = "<script type="text/javascript">alert('" + cleanMessage + "');</script>";  

It is showing me the error: ; Expected

And also on

page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);  

Err: The type or namespace name 'Alert' could not be found (are you missing a using directive or an assembly reference?)

What am I doing wrong here?

like image 267
RG-3 Avatar asked Apr 28 '11 21:04

RG-3


1 Answers

Here is an easy way:

Response.Write("<script>alert('Hello');</script>"); 
like image 116
Muhammad Akhtar Avatar answered Oct 07 '22 22:10

Muhammad Akhtar