Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JavaScript:ASP.NET代码隐藏中的Alert.Show(消息)_C#_Javascript_Asp.net - Fatal编程技术网

C# JavaScript:ASP.NET代码隐藏中的Alert.Show(消息)

C# JavaScript:ASP.NET代码隐藏中的Alert.Show(消息),c#,javascript,asp.net,C#,Javascript,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 Registrati

我正在读这个

我正在努力实现同样的目标。所以我创建了一个静态类,如下所示:

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); 
               } 
            } 
    }
}
错误:找不到类型或命名空间名称“Alert”(是否缺少using指令或程序集引用?

我做错了什么

type=“text/javascript”周围的引号将在您想要结束字符串之前结束字符串。在内部使用单引号可避免此问题

用这个

type='text/javascript'
type='text/javascript'您的代码无法编译。您拥有的字符串意外终止

string script = "<script type=";

string script=“您需要修复此行:

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

string script=“警报(“+cleanMessage+”);";

不工作的原因可能不止一个

1:你调用你的函数正确吗

Repository.Show("Your alert message");
2:尝试使用RegisterStartUpScript方法而不是scriptblock

3:如果您使用的是UpdatePanel,那么这可能也是一个问题

检查一下这里有一个简单的方法:

Response.Write("<script>alert('Hello');</script>");
Response.Write(“警报('Hello');”);

您可以在将客户端代码作为字符串参数发送后使用此方法

注意:我没有想出这个解决方案,但我自己在寻找方法时遇到了它,我只对它进行了一点编辑

它非常简单且有用,可以使用它执行多行javascript/jquery/…等或任何客户端代码

private void MessageBox(string msg)
{
    Label lbl = new Label();
    lbl.Text = "<script language='javascript'>" + msg + "')</script>";
    Page.Controls.Add(lbl);
}
private void消息框(字符串消息)
{
标签lbl=新标签();
lbl.Text=“+msg+”)”;
页面控件添加(lbl);
}
来源:

string script=“警报(“+cleanMessage+”);"; 
在这种情况下,您应该使用string.Format。这是更好的编码样式。对于您来说,它应该是:

string script = string.Format(@"<script type='text/javascript'>alert('{0}');</script>");
string script=string.Format(@“警报({0}”);”;
还要注意,当您应该转义“symbol”或改用撇号时。

请尝试以下方法:

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 */);
    } 
} 
在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
private void消息框(字符串消息)
{
标签lbl=新标签();
lbl.Text=string.Format(@“alert({0}”);”,msg);
页面控件添加(lbl);
}

如果您在页面上使用ScriptManager,则也可以尝试使用以下方法:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Your Message');", true);

调用消息框很简单,所以如果您想在后面编写代码或调用函数,我认为它更好,也可能不是。有一个进程,您可以只使用名称空间

using system.widows.forms;
然后,在要显示消息框的地方,只需简单地调用它,如C#,如:


此消息直接显示警报消息

ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);
此消息显示来自JavaScript函数的警报消息

ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
以下是两种以c代码隐藏的方式显示警报消息的方法,我认为这一行:

string cleanMessage = message.Replace("'", "\'"); 
不起作用,必须:

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

您需要用一个
\
来屏蔽
\
,用另一个
\
来屏蔽
\
从代码后面调用JavaScript函数

步骤1添加Javascript代码

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

如果希望在同一页上显示警报框,而不在空白页上显示,请尝试此操作

ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Sorry there are no attachments');", true);
您需要(查看“特殊字符”部分)。您可以通过在其前面添加斜杠来完成此操作:

string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
  Response.Write(script);
string script=“警报(“+cleanMessage+”);”;
响应。编写(脚本);

我使用它,只要页面不被重定向,它就可以工作。最好让它显示出来,然后等待用户单击OK,不管重定向如何

/// <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);
       }
    }    
}
//
///JavaScript警报类
/// 
公共静态类webMessageBox
{
/// 
///在浏览器中显示客户端JavaScript警报。
/// 
///要显示在警报中的消息。
公共静态无效显示(字符串消息)
{
//清除消息以允许使用单引号
字符串cleanMessage=message.Replace(“'”,“\\'”);
字符串wsScript=“警报(“+cleanMessage+”);”;
//获取正在执行的网页
Page Page=HttpContext.Current.CurrentHandler作为页面;
//检查处理程序是否是一个页面,以及该页面上的脚本是否未准备就绪
if(page!=null&&!page.ClientScript.IsClientScriptBlockRegistered(“警报”))
{
//RegisterStartupScript(this.GetType(),“MessageBox”,wsScript,true);
page.ClientScript.RegisterClientScriptBlock(typeof(webMessageBox),“警报”,wsScript,false);
}
}    
}

工作100%没有任何问题,不会重定向到另一个页面…我尝试复制此内容并更改您的消息

// 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());
ClientScript.RegisterStartupScript(Page.GetType(),“验证”,“警报('ID Exists'));

如果事件是页面加载事件,则仅调用脚本无法执行此操作

你需要打电话, 响应。编写(脚本)

所以如上所述,, string script=“警报(“+cleanMessage+”);”; 响应。编写(脚本)


将至少在页面加载事件中工作。

如果您想对代码隐藏文件进行处理,请尝试此操作

string popupScript = "<script language=JavaScript>";
popupScript += "alert('Your Massage');";

popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);
字符串popuscript=”“;
popupScript+=“警报(‘您的按摩’);”;
popupScript+=“”;
RegisterStartupScript(“PopupScript”,PopupScript);

您可以使用以下代码

 StringBuilder strScript = new StringBuilder();
 strScript.Append("alert('your Message goes here');");
 Page.ClientScript.RegisterStartupScript(this.GetType(),"Script", strScript.ToString(), true);

函数HideLabel(){
var秒=5;
setTimeout(函数(){
document.getElementById(“”.style.display=“无”;
},秒*1000);
};

Come-on@Tejs,休息一下:)连续第三个问题,您提前几秒钟到达:)谢谢!警报错误呢?这可能是被劫持代码的副作用。如果不修复之前的错误,就不可能说了。谢谢Tejs。但是我没有从上面的代码中得到任何提示。浏览器(firebug、开发者工具、蜻蜓等等)说了什么?脚本选项卡上有错误消息吗?谢谢!警报错误呢?谢谢你,安德里安。但是我没有从上面的代码中得到任何提示。你应该一次修复一件事。在修复字符串后是否仍会发生此错误?N
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
string cleanMessage = message.Replace("'", "\'"); 
string cleanMessage = message.Replace("'", "\\\'");
<script type="text/javascript" language="javascript">
    function Func() {
        alert("hello!")
    }
</script>
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true);
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Sorry there are no attachments');", true);
string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
  Response.Write(script);
/// <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);
       }
    }    
}
// 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());
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('ID Exists ')</script>");
string popupScript = "<script language=JavaScript>";
popupScript += "alert('Your Massage');";

popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);
 StringBuilder strScript = new StringBuilder();
 strScript.Append("alert('your Message goes here');");
 Page.ClientScript.RegisterStartupScript(this.GetType(),"Script", strScript.ToString(), true);
 <!--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 -->