gpt4 book ai didi

c# - JavaScript:来自 ASP.NET 代码隐藏的 Alert.Show(message)

转载 作者:IT王子 更新时间:2023-10-29 02:59:13 25 4
gpt4 key购买 nike

我正在读这个JavaScript: Alert.Show(message) From ASP.NET Code-behind

我正在尝试实现相同的功能。所以我创建了一个这样的静态类:

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);
}
}
}
}

在这一行:

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

它向我显示错误:;预计

还有在

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

错误:找不到类型或命名空间名称“Alert”(是否缺少 using 指令或程序集引用?)

我在这里做错了什么?

最佳答案

这里有一个简单的方法:

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

关于c# - JavaScript:来自 ASP.NET 代码隐藏的 Alert.Show(message),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5825165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com