在Asp.net 2.0里使用Atlas的UpdatePanel的时候遇到一个问题:在服务器端注册使用JS的脚本会无法使用,一般会提示 "Unknown error"
经过测试发现可以如此解决.
1.服务器端注册JS脚本的时候使用 Page.ClientScript.RegisterStartupScript() 或 Page.ClientScript.RegisterClientScriptBlock(),不使用Page.RegisterStartupScript()和Page.RegisterClientScriptBlock()
前两个方法是Asp.net 2.0里用来代替后面两个在Asp.net1.1里的两个注册脚本的方法的.参考:http://msdn2.microsoft.com/zh-CN/library/z9h4dk8y.aspx
http://www.microsoft.com/china/msdn/library/webservices/asp.net/JAVAwASP2.mspx?mfr=true
如下:
AtlasUpdatePanelTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AtlasUpdatePanelTest.aspx.cs" Inherits="AtlasUpdatePanelTest" %>
<!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">
<atlas:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />
<h4>
With an UpdatePanel</h4>
<!-- This section of the page is wrapped by an UpdatePanel. -->
<atlas:UpdatePanel ID="up" runat="server" Mode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Button ID="BtnTest" runat="server" OnClick="BtnTest_Click" Text="TestJavaScript" />
</ContentTemplate>
</atlas:UpdatePanel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="OutUpdatePanel" />
</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">
<atlas:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />
<h4>
With an UpdatePanel</h4>
<!-- This section of the page is wrapped by an UpdatePanel. -->
<atlas:UpdatePanel ID="up" runat="server" Mode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Button ID="BtnTest" runat="server" OnClick="BtnTest_Click" Text="TestJavaScript" />
</ContentTemplate>
</atlas:UpdatePanel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="OutUpdatePanel" />
</form>
</body>
</html>
AtlasUpdatePanelTest.aspx.cs
public partial class AtlasUpdatePanelTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}
protected void BtnTest_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "TestJS", @"alert(""Page.ClientScript.RegisterStartupScript"")", true);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "TestJS", @"alert(""Page.ClientScript.RegisterClientScriptBlock"")", true);
// Response.Write("<script>alert('a')</script>"); //无法在Atlas UpdatePanel里使用
//Page.RegisterStartupScript("Test", "<script>alert('Page.RegisterStartupScript')</script>"); ////无法在Atlas UpdatePanel里使用
}
protected void Button1_Click(object sender, EventArgs e)
{
//不在Atlas UpdatePanel 里可以使用
Response.Write("<script>alert('a')</script>");
Page.RegisterStartupScript("Test", "<script>alert('Page.RegisterStartupScript')</script>");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}
protected void BtnTest_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "TestJS", @"alert(""Page.ClientScript.RegisterStartupScript"")", true);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "TestJS", @"alert(""Page.ClientScript.RegisterClientScriptBlock"")", true);
// Response.Write("<script>alert('a')</script>"); //无法在Atlas UpdatePanel里使用
//Page.RegisterStartupScript("Test", "<script>alert('Page.RegisterStartupScript')</script>"); ////无法在Atlas UpdatePanel里使用
}
protected void Button1_Click(object sender, EventArgs e)
{
//不在Atlas UpdatePanel 里可以使用
Response.Write("<script>alert('a')</script>");
Page.RegisterStartupScript("Test", "<script>alert('Page.RegisterStartupScript')</script>");
}
}
诗词在线
http://www.chinapoesy.com
诗词在线 |唐诗|宋词|元曲|现代诗歌|外国诗歌
126在线阅读网
http://www.Read126.cn
126在线阅读网 人物传记、古典名著、历史书籍。。。
http://www.chinapoesy.com
诗词在线 |唐诗|宋词|元曲|现代诗歌|外国诗歌
126在线阅读网
http://www.Read126.cn
126在线阅读网 人物传记、古典名著、历史书籍。。。