How to use ClientScript.RegisterClientScriptBlock with inline code?

I have an aspx page where there is no code. Server code written inside a tag with a runat server attribute.

If i use

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

in the event page_load(), it just prints GetEmail();when the page loads

My code looks like

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

Thanks in advance.

+2
source share
1 answer

You need to pass RegisterClientScriptBlock ()true as the last argument so that your client code is wrapped in :<script>

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

Source: https://habr.com/ru/post/1606790/


All Articles