1.先說一下ScriptManager控件吧,用過Ajax的都知道他是什么了就不用我多說了吧呵呵?。?!
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", "你好");
這兩句可以在當前控件的下面輸出一個你好,呵呵,我建議大家以后都有這種方式做,不要再用Response.Write()
它只能用在方法里,"你好"的地方 也可以寫成Script代碼,不用加頭但是呢要在后面加一個對數(shù)true
看這個吧
csm.RegisterStartupScript(this.GetType(), "sufei", "alert('蘇飛你好啊fdfd')", 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"表示在發(fā)生錯誤時不按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("呵呵");
}
}
其實怎么說呢,看到這里ClientScriptManager和ScriptManager基本上沒有什么區(qū)別
但看只是前者是一個控件
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()方法,因為它會破壞頁面的標準(會出現(xiàn)在頁面的上方)
* 破壞了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('你真的要提交界面嗎');");
}
}
//添加數(shù)組
protected void Button1_Click(object sender, EventArgs e)
{
//得到一個ClientScriptManager對象
ClientScriptManager csm = this.ClientScript;
//這個方法可以在前臺注冊一個var aa = new Array(4ghg,2,3);數(shù)組
csm.RegisterArrayDeclaration("aa", "4ghg,2,3");
}
//輸出一個提示信息
protected void Button2_Click(object sender, EventArgs e)
{
//得到一個ClientScriptManager對象
ClientScriptManager csm = this.ClientScript;
// 輸出一個提示信息啊后一個參數(shù)的意思是是否自動添加JS頭
csm.RegisterClientScriptBlock(this.GetType(), "提示信息", "alert('蘇飛你好啊')", true);
//出現(xiàn)的位置不同上面的是在頁面上方顯示,而這個是在當前控件的下面顯示
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");
}
}