如何使用ClientScript.RegisterClientScriptBlock与在线code? [英] How to use ClientScript.RegisterClientScriptBlock with Inline code?

查看:139
本文介绍了如何使用ClientScript.RegisterClientScriptBlock与在线code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有aspx页面里没有code后面。服务器端code里面有RUNAT服务器属性标记写入。

I have aspx page where there is no code behind. Server side Code written inside tag with runat server attribute.

如果我使用

ClientScript.RegisterClientScriptBlock(this.GetType(), "Email", "GetEmail();");

的Page_Load()事件,它只是打印 GetEmail(); 时,页面加载

in page_load() event, it just print GetEmail(); when page load

我的code看起来像

<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>

先谢谢了。

推荐答案

您需要通过真正作为最后一个参数的的RegisterClientScriptBlock()为了让您的客户端code被包裹在一个 &LT;脚本&GT; 元素:

You need to pass true as the last argument to RegisterClientScriptBlock() in order for your client-side code to be wrapped in a <script> element:

protected void Page_Load(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(GetType(), "Email",
        "GetEmail();", true);
} 

这篇关于如何使用ClientScript.RegisterClientScriptBlock与在线code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆