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

I am reading this JavaScript: Alert.Show (message) From ASP.NET code

I am trying to implement the same thing. 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>"; 

This shows me an error:; expected

As well as

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

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

What am I doing wrong here?

+51
javascript c #
Apr 28 2018-11-21T00:
source share
26 answers

Here is an easy way:

 Response.Write("<script>alert('Hello');</script>"); 
+106
Apr 29
source share
 string script = string.Format("alert('{0}');", cleanMessage); if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) { page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */); } 
+22
Apr 28 '11 at 21:45
source share

This message shows a warning message directly.

 ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true); 

This message shows a JavaScript function message.

 ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true); 

These are two ways to display warning messages in C # code behind

+18
Sep 21 '13 at 9:16
source share

if you use ScriptManager on the page, you can also try:

 System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Your Message');", true); 
+9
Jul 10 '13 at 17:52
source share

Try this method:

 public static void Show(string message) { string cleanMessage = message.Replace("'", "\'"); Page page = HttpContext.Current.CurrentHandler as Page; string script = string.Format("alert('{0}');", cleanMessage); if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) { page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */); } } 

In Vb.Net

 Public Sub Show(message As String) Dim cleanMessage As String = message.Replace("'", "\'") Dim page As Page = HttpContext.Current.CurrentHandler Dim script As String = String.Format("alert('{0}');", cleanMessage) If (page IsNot Nothing And Not page.ClientScript.IsClientScriptBlockRegistered("alert")) Then page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, True) ' /* addScriptTags */ End If End Sub 
+5
May 15, '13 at 22:45
source share

Your code does not compile. A line that you end unexpectedly

 string script = "<script type="; 

It is effective what you wrote. You need to avoid double quotes:

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

Such a thing should be painfully obvious, since the coloring of the source code should be completely unclenched.

+4
Apr 28 2018-11-21T00:
source share

There could be several reasons for not working.

1: Are you calling your function correctly? those.

 Repository.Show("Your alert message"); 

2: try using the RegisterStartUpScript method instead of scriptblock.

3: If you are using UpdatePanel, this can be a problem.

Mark it (topic 3.2)

+4
Apr 28 '11 at 23:16
source share
 ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('ID Exists ')</script>"); 
+4
Jul 19 '16 at 7:25
source share

And I think the line:

 string cleanMessage = message.Replace("'", "\'"); 

does not work, it should be:

 string cleanMessage = message.Replace("'", "\\\'"); 

You need to mask \ with \ and ' with another \ .

+3
Oct 07 '13 at 19:51
source share

JavaScript function call from code

Step 1 Add Your Javascript Code

 <script type="text/javascript" language="javascript"> function Func() { alert("hello!") } </script> 

Step 2 Add 1 Script Manager to your web form and add 1 button too

Step 3 Add this code to the button click event

 ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true); 
+3
Mar 19 '14 at 21:11
source share

Try this if you want to display a warning window to display on the same page, without displaying on a blank page.

 ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Sorry there are no attachments');", true); 
+3
Dec 03 '14 at 12:12
source share

You need to fix this line:

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

And also this one:

 RegisterClientScriptBlock("alert", script); //lose the typeof thing 
+2
Apr 28 2018-11-21T00:
source share
 string script = string.Format("alert('{0}');", cleanMessage); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", script );", true); 
+2
Apr 23 2018-12-12T00:
source share

It works 100% without any problems and does not redirect to another page ... I tried to just copy this and change your message

 // Initialize a string and write Your message it will work string message = "Helloq World"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("alert('"); sb.Append(message); sb.Append("');"); ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString()); 
+2
May 26 '16 at 19:24
source share

The quotes around type = "text / javascript" end your line before you want to. Use single quotes inside to avoid this problem.

Use this

  type = 'text / javascript' 
+1
Apr 28 2018-11-21T00:
source share

to try:

 string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>"; 
+1
Apr 28 2018-11-21T00:
source share

This method can be used after sending the client code as a string parameter.

NOTE I did not come up with this solution, but I came across it when I was looking for a way to do it myself, I only edited it a little.

It is very simple and useful and can use it to execute more than 1 line of javascript / jquery / ... or any client code

 private void MessageBox(string msg) { Label lbl = new Label(); lbl.Text = "<script language='javascript'>" + msg + "')</script>"; Page.Controls.Add(lbl); } 

source: stack overflow

+1
Oct 31 '12 at 15:16
source share
 string script = "<script type="text/javascript">alert('" + cleanMessage + "');</script>"; 

In this case, you should use string.Format. This is the best coding style. For you, it will be:

 string script = string.Format(@"<script type='text/javascript'>alert('{0}');</script>"); 

Also note that when you should avoid the character or use the apostrophe instead.

+1
Nov 26
source share
 private void MessageBox(string msg) { Label lbl = new Label(); lbl.Text = string.Format(@"<script type='text/javascript'>alert('{0}');</script>",msg); Page.Controls.Add(lbl); } 
+1
May 24 '13 at 11:41
source share

You need to avoid your quotes (see the section "Special Characters"). You can do this by adding a slash in front of them:

 string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>"; Response.Write(script); 
+1
Feb 27 '15 at 14:18
source share

I use this and it works until the page is redirected subsequently. It would be nice to show this and wait for the user to click OK, regardless of the redirect.

 /// <summary> /// A JavaScript alert class /// </summary> public static class webMessageBox { /// <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 wsScript = "<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")) { //ClientScript.RegisterStartupScript(this.GetType(), "MessageBox", wsScript, true); page.ClientScript.RegisterClientScriptBlock(typeof(webMessageBox), "alert", wsScript, false); } } } 
+1
Sep 08 '15 at 2:06
source share

Calling a script just cannot do this if the event is a PAGE LOAD event specifically.

you need to call, Response.Write (script);

as above, string script = "alert ('" + cleanMessage + "');"; Response.Write (script);

will work, at least for the page load event.

+1
Jul 27 '16 at 9:00
source share

if u Want To massage on your code for the file, try this

 string popupScript = "<script language=JavaScript>"; popupScript += "alert('Your Massage');"; popupScript += "</"; popupScript += "script>"; Page.RegisterStartupScript("PopupScript", popupScript); 
+1
Jan 23 '17 at 5:17
source share

you can use the following code.

  StringBuilder strScript = new StringBuilder(); strScript.Append("alert('your Message goes here');"); Page.ClientScript.RegisterStartupScript(this.GetType(),"Script", strScript.ToString(), true); 
+1
Jan 23 '17 at 5:25
source share
  <!--Java Script to hide alert message after few second --> <script type="text/javascript"> function HideLabel() { var seconds = 5; setTimeout(function () { document.getElementById("<%=divStatusMsg.ClientID %>").style.display = "none"; }, seconds * 1000); }; </script> <!--Java Script to hide alert message after few second --> 
0
May 25 '19 at 5:16
source share

its just calling a message box, so if you want to code a code or call a function, I think it is better or maybe not. There is a process, you can just use the namespace

 using system.widows.forms; 

then when you want to show the message box, just call it as simple as in C #, for example:

 messagebox.show("Welcome"); 
-2
Aug 20 '13 at 20:54 on
source share



All Articles