RG-*_*G-3 51 javascript c# asp.net
我正在阅读这个JavaScript:Alert.Show(消息)来自ASP.NET代码隐藏
我正在努力实现同样的目标.所以我创建了一个这样的静态类:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
string script = "<script type="text/javascript">alert('" + cleanMessage + "');</script>";
Run Code Online (Sandbox Code Playgroud)
它向我显示错误:; 预期
还有
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
Run Code Online (Sandbox Code Playgroud)
错误:找不到类型或命名空间名称"警报"(您是否缺少using指令或程序集引用?)
我在这做错了什么?
Muh*_*tar 110
这是一个简单的方法:
Response.Write("<script>alert('Hello');</script>");
Run Code Online (Sandbox Code Playgroud)
cem*_*cem 22
string script = string.Format("alert('{0}');", cleanMessage);
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
}
Run Code Online (Sandbox Code Playgroud)
use*_*316 18
此消息直接显示警报消息
ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);
Run Code Online (Sandbox Code Playgroud)
此消息显示来自JavaScript函数的警报消息
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
Run Code Online (Sandbox Code Playgroud)
这两种方法可以在后面的c#代码中显示警报消息
小智 9
如果您在页面上使用ScriptManager,那么您也可以尝试使用它:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Your Message');", true);
Run Code Online (Sandbox Code Playgroud)
试试这个方法:
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 */);
}
}
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)