C# 从代码隐藏页面显示Javascript不起作用!
我想在用户提交页面时显示Javascript。我通过代码隐藏调用这个Javascript(我认为这很容易)。这是我的密码:C# 从代码隐藏页面显示Javascript不起作用!,c#,javascript,jquery,.net,asp.net,C#,Javascript,Jquery,.net,Asp.net,我想在用户提交页面时显示Javascript。我通过代码隐藏调用这个Javascript(我认为这很容易)。这是我的密码: MessageBox1("Testing my Message"); //Calling Function! private void MessageBox1(string msg) //This is in the code behind of the page. { // Cleans the message to allow sing
MessageBox1("Testing my Message"); //Calling Function!
private void MessageBox1(string msg) //This is in the code behind of the page.
{
// Cleans the message to allow single quotation marks
string cleanMessage = msg.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(CommSetup), "alert", script);
}
}
MessageBox1(“测试我的消息”)//调用函数!
private void MessageBox1(string msg)//这在页面的代码后面。
{
//清除消息以允许使用单引号
字符串cleanMessage=msg.Replace(“'”,“\\'”);
string script=“警报(“+cleanMessage+”);”;
//获取正在执行的网页
Page Page=HttpContext.Current.CurrentHandler作为页面;
//检查处理程序是否是一个页面,以及该页面上的脚本是否未准备就绪
if(page!=null&&!page.ClientScript.IsClientScriptBlockRegistered(“警报”))
{
page.ClientScript.RegisterClientScriptBlock(typeof(CommSetup),“警报”,脚本);
}
}
这不管用…我做错了什么?谢谢大家! 使用ClientScriptManager而不是文字:
Page.ClientScriptManager.RegisterStarupScript("startup",
"<script language='javascript'>window.location=''; window.alert('" + msg.Replace("'", "\\'") + "') </script>", false);
也是
HTH.使用ClientScriptManager而不是文字:
Page.ClientScriptManager.RegisterStarupScript("startup",
"<script language='javascript'>window.location=''; window.alert('" + msg.Replace("'", "\\'") + "') </script>", false);
也是
HTH.使用此静态方法弹出警报:
public static void JS_Alert(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(Utilities), "alert", script);
}
}
公共静态无效JS_警报(字符串消息)
{
//清除消息以允许使用单引号
字符串cleanMessage=message.Replace(“'”,“\\'”);
string script=“警报(“+cleanMessage+”);”;
//获取正在执行的网页
Page Page=HttpContext.Current.CurrentHandler作为页面;
//检查处理程序是否是一个页面,以及该页面上的脚本是否未准备就绪
if(page!=null&&!page.ClientScript.IsClientScriptBlockRegistered(“警报”))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Utilities),“alert”,script);
}
}
编辑:“实用程序”应该是放置此方法的类的名称。我的恰好叫做公用事业。如果您将其放入代码隐藏部分类中,则通常会调用它,不管您的网页最后用.cs调用什么。使用此静态方法弹出警报:
public static void JS_Alert(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(Utilities), "alert", script);
}
}
公共静态无效JS_警报(字符串消息)
{
//清除消息以允许使用单引号
字符串cleanMessage=message.Replace(“'”,“\\'”);
string script=“警报(“+cleanMessage+”);”;
//获取正在执行的网页
Page Page=HttpContext.Current.CurrentHandler作为页面;
//检查处理程序是否是一个页面,以及该页面上的脚本是否未准备就绪
if(page!=null&&!page.ClientScript.IsClientScriptBlockRegistered(“警报”))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Utilities),“alert”,script);
}
}
编辑:“实用程序”应该是放置此方法的类的名称。我的恰好叫做公用事业。如果你把它放在代码隐藏部分类中,它通常会被调用,不管你的网页最后用.cs调用什么。发布呈现的javascript以及服务器端代码。发布呈现的javascript以及服务器端代码。有关其他信息,请参阅编辑。我做到了,请参阅我的编辑代码。它没有显示我的脚本…它进入函数内部,但没有向我显示脚本…测试浏览器中是否启用了JavaScript?有关其他信息,请参阅编辑。我已经这样做了,请参阅我的编辑代码。它没有显示我的脚本…它进入函数内部,但没有向我显示脚本…测试浏览器中是否启用了JavaScript?