在
AJAX
支持的网站中使用想使用
Response.Wrie(“”);
或
Page. RegisterStartupScript ();
弹出一些提示对话框,没有效果。有如下两种解决方法:
(1) System.Web.UI.ScriptManager.RegisterStartupScript来替代Page.ClientScript.RegisterStartupScript
==>函数原型:
System.Web.UI.ScriptManager.RegisterStartupScript
(
Contrl control, Type type,string key,string script, bool addScriptTags
)
;
==>参数:
control
-----------------要要注册此段
javascript
语句的控件
ID,
如下面示例的按钮
ID btnUnReport
type--------------------
一般直接用
this.GetType()
即可
key---------------------
为要执行的
javascirpt
语句起的名字,可以随便起,类似控件的
name
属性
script------------------javascript
语句
addScriptTags------
为
true
时,前边的
script
参数可以不用再写
javascript
标签;为
false,
则需自己为
script
参数添加
<script language=’javascript’></script>
标签。
注:要多传一个Control参数,如果这个control在UpdatePanel里,则运行脚步,如果这个control不在UpdatePanel则不允许这段脚步。
==>Eg.
System.Web.UI.ScriptManager.RegisterStartupScript
(btnUnReport, this.GetType(), "unReport", "alert('
撤销上报操作失败,请勾选要上交的课题!
');window.close();", true);
(2)注册DataItem方法:
示例:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
WebForm1.aspx.cs
"
Inherits
=
"
WebForm1
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< asp:ScriptManager ID = " ScriptManager1 " runat = " server " ></ asp:ScriptManager >
< script type = " text/javascript " >
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(
function(sender, e)
{
var dataItem = e.get_dataItems()["<%= this.UpdatePanel1.ClientID %>"]();
alert(dataItem.Name);
} );
</ script >
< div >
< asp:UpdatePanel ID = " UpdatePanel1 " runat = " server " >
< ContentTemplate >
< asp:Button ID = " test " runat = " server " Text = " test " OnClick = " test_Click " />
</ ContentTemplate >
</ asp:UpdatePanel >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< asp:ScriptManager ID = " ScriptManager1 " runat = " server " ></ asp:ScriptManager >
< script type = " text/javascript " >
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(
function(sender, e)
{
var dataItem = e.get_dataItems()["<%= this.UpdatePanel1.ClientID %>"]();
alert(dataItem.Name);
} );
</ script >
< div >
< asp:UpdatePanel ID = " UpdatePanel1 " runat = " server " >
< ContentTemplate >
< asp:Button ID = " test " runat = " server " Text = " test " OnClick = " test_Click " />
</ ContentTemplate >
</ asp:UpdatePanel >
</ div >
</ form >
</ body >
</ html >
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void test_Click(object sender, EventArgs e)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
ScriptManager.GetCurrent(this.Page).RegisterDataItem(this.UpdatePanel1, "var _f = function(){alert('Hello World!');}; _f;", true);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void test_Click(object sender, EventArgs e)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
ScriptManager.GetCurrent(this.Page).RegisterDataItem(this.UpdatePanel1, "var _f = function(){alert('Hello World!');}; _f;", true);
}
}
(3) 使用Javascript的非模态对话框弹出提示
Eg.
Page.RegisterStartupScript("alert", "<script language='javascript'>window.showModelessDialog(/"javascript:alert('
撤销上报操作失败,请勾选要上交的课题!
');window.close();/",/"/",/"status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;/")</script>");