![](/img/trans.png)
[英]Alternative to ClientScript.RegisterClientScriptBlock?
[英]How to use ClientScript.RegisterClientScriptBlock with Inline code?
我有aspx页面,其中没有任何代码。 服务器端代码在标签内部写入,带有runat服务器属性。
如果我使用
ClientScript.RegisterClientScriptBlock(this.GetType(), "Email", "GetEmail();");
在page_load()
事件中,它只打印GetEmail();
页面加载时
我的代码看起来像
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript">
function GetEmail()
{
alert('hi');
}
</script>
</head>
<body>
<form id="form1" runat="server">
Some control here
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Email", "GetEmail();");
}
</script>
提前致谢。
您需要将true
作为最后一个参数传递给RegisterClientScriptBlock() ,以便将客户端代码包装在<script>
元素中:
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(GetType(), "Email",
"GetEmail();", true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.