Ajax中使用Response.Write输出javascript脚本出错的解决方案
2010-07-17 14:35
465 查看
这一段时间在使用Ajax将老ASP.NET程序升级为Ajax程序,在升级中发现先前页面中使用Response.Write("<script>alert('数据添加成功!')</script>")的代码执行时出错.上网上查找解决办法,网上大部分的解决语句是改为 ScriptManager.RegisterStartupScript(updatePanel1, typeof(UpdatePanel), "aaa", "<script>alert('数据添加成功!')</script>",true);包括陈黎夫著的<<ASP.NET AJAX>>中P104页中解决方法.
因为我们现在使用Visual Studio 2008 Beta2中的Ajax(3.5),我在使用上述的语句进行处理时,系统并没有预期的脚本运行来跳出对话框,不知是不是Ajax 3.5是不是有改动.后来经过尝试将ScriptManager.RegisterStartupScript(updatePanel1, typeof(UpdatePanel), "aaa", "<script>alert('数据添加成功!')</script>",true);中最后的true变为ScriptManager.RegisterStartupScript(updatePanel1, typeof(UpdatePanel), "aaa", "<script>alert('数据添加成功!')</script>",false);就可以了.下面写一个通用的消息提示类.
public class MessageBox
{
private HttpContext myContext = null;
public MessageBox(HttpContext CurrentContext)
{
myContext = CurrentContext;
}
/// <summary>
/// 自定义弹出窗口内容
/// </summary>
/// <param name="msg"></param>
public void Show(string msg)
{
//myContext.Response.Write("<script>alert('" + msg + "');</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');</script>", false);
}
/// <summary>
/// 自定义弹出窗口内容并转向一个新的页面
/// </summary>
/// <param name="msg">自定义消息</param>
/// <param name="Url">需要转到的新页面</param>
public void Show(string msg,string Url)
{
//myContext.Response.Write("<script>alert('" + msg + "');javascript:location='"+Url+"';</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:location='" + Url + "';</script>", false);
}
/// <summary>
/// 自定义弹出窗口内容,自定义是否关闭当前页面
/// </summary>
/// <param name="msg"></param>
/// <param name="close"></param>
public void Show(string msg, bool close)
{
if (close)
{
// myContext.Response.Write("<script>alert('" + msg + "');javascript:window.close();</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:window.close();</script>", false);
}
else
{
//myContext.Response.Write("<script>alert('" + msg + "');</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:window.close();</script>", false);
}
}
}
--------------转自"飞飞航的老家"
因为我们现在使用Visual Studio 2008 Beta2中的Ajax(3.5),我在使用上述的语句进行处理时,系统并没有预期的脚本运行来跳出对话框,不知是不是Ajax 3.5是不是有改动.后来经过尝试将ScriptManager.RegisterStartupScript(updatePanel1, typeof(UpdatePanel), "aaa", "<script>alert('数据添加成功!')</script>",true);中最后的true变为ScriptManager.RegisterStartupScript(updatePanel1, typeof(UpdatePanel), "aaa", "<script>alert('数据添加成功!')</script>",false);就可以了.下面写一个通用的消息提示类.
public class MessageBox
{
private HttpContext myContext = null;
public MessageBox(HttpContext CurrentContext)
{
myContext = CurrentContext;
}
/// <summary>
/// 自定义弹出窗口内容
/// </summary>
/// <param name="msg"></param>
public void Show(string msg)
{
//myContext.Response.Write("<script>alert('" + msg + "');</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');</script>", false);
}
/// <summary>
/// 自定义弹出窗口内容并转向一个新的页面
/// </summary>
/// <param name="msg">自定义消息</param>
/// <param name="Url">需要转到的新页面</param>
public void Show(string msg,string Url)
{
//myContext.Response.Write("<script>alert('" + msg + "');javascript:location='"+Url+"';</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:location='" + Url + "';</script>", false);
}
/// <summary>
/// 自定义弹出窗口内容,自定义是否关闭当前页面
/// </summary>
/// <param name="msg"></param>
/// <param name="close"></param>
public void Show(string msg, bool close)
{
if (close)
{
// myContext.Response.Write("<script>alert('" + msg + "');javascript:window.close();</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:window.close();</script>", false);
}
else
{
//myContext.Response.Write("<script>alert('" + msg + "');</script>");
ScriptManager.RegisterStartupScript((System.Web.UI.Page)myContext.CurrentHandler, typeof(System.Web.UI.Page), "aaa", "<script>alert('" + msg + "');javascript:window.close();</script>", false);
}
}
}
--------------转自"飞飞航的老家"
相关文章推荐
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- AJAX下不能使用RESPONSE.WRITE 脚本的解决方案
- AJAX下不能使用RESPONSE.WRITE 脚本的解决方案
- 使用PrintWriter out=response.getWriter();输出script脚本时乱码解决
- 在atlas(ajax)中请不要使用Response.Write,请使用ClientScript.RegisterClientScriptBlock替换
- 解决AJAX中使用UpdatePanel后再用Response.Write();无法弹出对话框问题
- 使用JavaScript语言的循环语句和document.write()语句在网页中输出4行三列的表格
- (转)解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题 3法
- 页面使用Ajax控件,后台不能调用前台Javascript脚本弹出消息框的解决办法
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
- 使用jQuery异步加载 JavaScript脚本解决方案
- 解决response.write输出脚本后出现页面变形的问题
- 怎么使用response.write来做一个javascript的alert弹出窗口
- 使用Response.Write(<script language=javascript>alert的注意事项