我可能在这里错过了一些基本的东西,但对我来说似乎有点棘手和令人困惑,所以这里...
为了演示这个问题,我有以下示例.aspx页
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<script type=text/javascript>alert('Alert from response.write')" & ";</sc" & "ript>")
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<script type=text/javascript> helloWorld(); </sc" & "ript>")
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<script type=text/javascript> helloWorld(); </sc" & "ript>")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function helloWorld()
{
alert('hello!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat=server ID="Button1" text=1 OnClick="Button1_Click" />
<asp:Button runat=server ID="Button2" text=2 OnClick="Button2_Click" />
<asp:Button runat=server ID="Button3" text=3 OnClick="Button3_Click" OnClientClick="helloWorld();" />
<asp:Button runat=server ID="Button4" text=4/>
</div>
</form>
</body>
</html>
因此,我有3个按钮,第一个调用response.write来执行JS警报。
第二个尝试调用head标记中定义的helloWorld()。这不起作用。
第三个在response.write和onClientClick()中都称为helloWorld()-仅onClientClick起作用。
有人可以解释为什么我不能使用response.write方法调用helloWorld()吗?
干杯:D
最佳答案
我认为Response.Write可能会擦除PostBack中的helloWorld()定义。尝试使用PlaceHolder将脚本标签插入HTML。
丰富
关于javascript - ASP.NET的Javascript范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1073354/