最近也在看一些asp.net ajax 的资料,在网上看到很多人都在问如何在updatepanel中注册脚本,我也试了一下,不过总是不行,就看了好多资料,最后才知道自己没有完全理解 ScriptManager.RegisterClientScriptBlock(Control control,Type type,string key, string script,bool addScriptTags),其中的各个参数。(注:RegisterClientScriptBlock是 ScriptManager的一个静态方法) 参数详解: control (Control) :这个参数是注册脚本块的控件.如果你是在updatepanel中注册时,即updatepanel (应该写updatepanel的ID). type (Type) :这个参数是注册脚本块控件的类型,即updatepanel的类型。 key (String) :这个参数是脚本酷块的惟一标识(关键字) script (String) :这个参数是要注册的脚本字符串. addScriptTags (Boolean) :这个参数表示是否要在您的字符串两边使用“<script>”和“</script>”包围起来.
然后这是我写的一个简单的例子: Html代码: <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css" rel="stylesheet" type="text/css" /></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
CS代码: protected void Button1_Click(object sender, EventArgs e) { ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "test", "alert('test');", true);
}
以上是我的理解,如果有不正确的地方,请大家纠正,我先放到首页,其实没有什么技术含量,主要是用来让那些还不知道如何注册的朋友看到.过后dohu可以删了。http://www.cnblogs.com/sunlife/archive/2006/11/05/550520.html