阅读全文:http://www.cckan.net/forum.php?mod=viewthread&tid=59
1.先说一下ScriptManager控件吧,用过Ajax的都知道他是什么了就不用我多说了吧呵呵!!!
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", "你好");
csm.RegisterStartupScript(this.GetType(), "sufei", "你好");
这两句可以在当前控件的下面输出一个你好,呵呵,我建议大家以后都有这种方式做,不要再用Response.Write()
它只能用在方法里,"你好"的地方 也可以写成Script代码,不用加头但是呢要在后面加一个对数true
看这个吧
csm.RegisterStartupScript(this.GetType(), "sufei", "alert('苏飞你好啊fdfd')", true);
csm.RegisterStartupScript(this.GetType(), "sufei", " document.write ('你好可以弹出的啊!!!')", true);
csm.RegisterStartupScript(this.GetType(), "sufei", " document.write ('你好可以弹出的啊!!!')", true);
下面是Aspx源里的代码,大家复制就可以了
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScriptManager.aspx.cs" Inherits="ClientScriptManager_ScriptManager" %>
<!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>苏飞—ScriptManager</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- AllowCustomErrorsRedirect="false"表示在发生错误时不按Web。Config里家配制的转到相应的错误界面当然True就是是了,也是默认的-->
<asp:ScriptManager ID="ScriptManager1" runat="server" AllowCustomErrorsRedirect="false">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode ="Conditional">
<ContentTemplate>
<%=DateTime .Now %>
<asp:Button ID="Button1" runat="server" Text="第一个时间" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<%--在这里是不会弹出对话框的,因为只有UpdatePanel刷新时才会取得值--%>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode ="Conditional" >
<ContentTemplate>
<%=DateTime .Now %>
<asp:Button ID="Button2" runat="server" Text="第二个时间"
onclick="Button2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<hr/>
<asp:Button ID="Button3" runat="server" Text="刷新所有" onclick="Button3_Click" />
</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>苏飞—ScriptManager</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- AllowCustomErrorsRedirect="false"表示在发生错误时不按Web。Config里家配制的转到相应的错误界面当然True就是是了,也是默认的-->
<asp:ScriptManager ID="ScriptManager1" runat="server" AllowCustomErrorsRedirect="false">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode ="Conditional">
<ContentTemplate>
<%=DateTime .Now %>
<asp:Button ID="Button1" runat="server" Text="第一个时间" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<%--在这里是不会弹出对话框的,因为只有UpdatePanel刷新时才会取得值--%>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode ="Conditional" >
<ContentTemplate>
<%=DateTime .Now %>
<asp:Button ID="Button2" runat="server" Text="第二个时间"
onclick="Button2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<hr/>
<asp:Button ID="Button3" runat="server" Text="刷新所有" onclick="Button3_Click" />
</div>
</form>
</body>
</html>
CS下的代码
代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ClientScriptManager_ScriptManager : System.Web.UI.Page
{
/// <summary>
/// 苏飞 2009 03 11
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//第一个UpdatePanel
protected void Button1_Click(object sender, EventArgs e)
{
//要注册代码的控件是UpdatePanel1
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "sufei1", "alert(111)", true);
//要注册代码的控件是UpdatePanel2
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "sufei2", "alert(222)", true);
//要注册代码的控件是Page下面的代码 不管UpdatePanel会不会刷新都会输出
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "sufei3", "alert(111)", true);
}
//第二个UpdatePanel
protected void Button2_Click(object sender, EventArgs e)
{
//要注册代码的控件是UpdatePanel1
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "sufei3", "alert(111)", true);
//要注册代码的控件是UpdatePanel2
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "sufei4", "alert(222)", true);
//throw new Exception("呵呵");
}
protected void Button3_Click(object sender, EventArgs e)
{
throw new Exception("呵呵");
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ClientScriptManager_ScriptManager : System.Web.UI.Page
{
/// <summary>
/// 苏飞 2009 03 11
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//第一个UpdatePanel
protected void Button1_Click(object sender, EventArgs e)
{
//要注册代码的控件是UpdatePanel1
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "sufei1", "alert(111)", true);
//要注册代码的控件是UpdatePanel2
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "sufei2", "alert(222)", true);
//要注册代码的控件是Page下面的代码 不管UpdatePanel会不会刷新都会输出
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "sufei3", "alert(111)", true);
}
//第二个UpdatePanel
protected void Button2_Click(object sender, EventArgs e)
{
//要注册代码的控件是UpdatePanel1
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "sufei3", "alert(111)", true);
//要注册代码的控件是UpdatePanel2
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "sufei4", "alert(222)", true);
//throw new Exception("呵呵");
}
protected void Button3_Click(object sender, EventArgs e)
{
throw new Exception("呵呵");
}
}
其实怎么说呢,看到这里ClientScriptManager和ScriptManager基本上没有什么区别
但看只是前者是一个控件
ClientScriptManager给出一点代码大家看一下有什么不同吧
代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class RequiredFieldValidator_Default : System.Web.UI.Page
{
/// <summary>
/// 苏飞 2009 03 11
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/*禁止使用Response.Write()方法,因为它会破坏页面的标准(会出现在页面的上方)
* 破坏了Asp.Net的模型,所有建议大家都不要使用
* 我们有强大的Page,Control模型就没有必要再用Response.Write()这样的方法,
* 如果你是一个好的程序员一个负责的程序员您就不要用这个方法
* 用下面我写的方法来完成 ClientScriptManager csm = this.ClientScript;只能在方法里不能写在类的上方
* //Response.Write("<script>alert('"+TextBox1 .Text .ToString ().Trim ()+"')</script>");
*/
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterOnSubmitStatement(this.GetType(), "sufei1", "return window.confirm('你真的要提交界面吗');");
}
}
//添加数组
protected void Button1_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
//这个方法可以在前台注册一个var aa = new Array(4ghg,2,3);数组
csm.RegisterArrayDeclaration("aa", "4ghg,2,3");
}
//输出一个提示信息
protected void Button2_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
// 输出一个提示信息啊后一个参数的意思是是否自动添加JS头
csm.RegisterClientScriptBlock(this.GetType(), "提示信息", "alert('苏飞你好啊')", true);
//出现的位置不同上面的是在页面上方显示,而这个是在当前控件的下面显示
csm.RegisterStartupScript(this.GetType(), "sufei", "alert('苏飞你好啊fdfd')", true);
csm.RegisterStartupScript(this.GetType(), "sufei", " document.write ('fdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfd')", true);
}
//导入一个文件
protected void Button3_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterClientScriptInclude("sufei", "../App_Themes/personnel_Css/forum.css");
}
//为一个控件添加一个属性
protected void Button4_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterExpandoAttribute(this.Button4.ClientID, "sufei", "苏飞");
//同时注册一个隐藏的值
//csm.RegisterHiddenField("kdfjkdfjkdfjlsjfl", "fdklfjldfldkf");
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class RequiredFieldValidator_Default : System.Web.UI.Page
{
/// <summary>
/// 苏飞 2009 03 11
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/*禁止使用Response.Write()方法,因为它会破坏页面的标准(会出现在页面的上方)
* 破坏了Asp.Net的模型,所有建议大家都不要使用
* 我们有强大的Page,Control模型就没有必要再用Response.Write()这样的方法,
* 如果你是一个好的程序员一个负责的程序员您就不要用这个方法
* 用下面我写的方法来完成 ClientScriptManager csm = this.ClientScript;只能在方法里不能写在类的上方
* //Response.Write("<script>alert('"+TextBox1 .Text .ToString ().Trim ()+"')</script>");
*/
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterOnSubmitStatement(this.GetType(), "sufei1", "return window.confirm('你真的要提交界面吗');");
}
}
//添加数组
protected void Button1_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
//这个方法可以在前台注册一个var aa = new Array(4ghg,2,3);数组
csm.RegisterArrayDeclaration("aa", "4ghg,2,3");
}
//输出一个提示信息
protected void Button2_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
// 输出一个提示信息啊后一个参数的意思是是否自动添加JS头
csm.RegisterClientScriptBlock(this.GetType(), "提示信息", "alert('苏飞你好啊')", true);
//出现的位置不同上面的是在页面上方显示,而这个是在当前控件的下面显示
csm.RegisterStartupScript(this.GetType(), "sufei", "alert('苏飞你好啊fdfd')", true);
csm.RegisterStartupScript(this.GetType(), "sufei", " document.write ('fdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfd')", true);
}
//导入一个文件
protected void Button3_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterClientScriptInclude("sufei", "../App_Themes/personnel_Css/forum.css");
}
//为一个控件添加一个属性
protected void Button4_Click(object sender, EventArgs e)
{
//得到一个ClientScriptManager对象
ClientScriptManager csm = this.ClientScript;
csm.RegisterExpandoAttribute(this.Button4.ClientID, "sufei", "苏飞");
//同时注册一个隐藏的值
//csm.RegisterHiddenField("kdfjkdfjkdfjlsjfl", "fdklfjldfldkf");
}
}