2

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

If i use

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

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

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>

1 Answer 1

7

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);
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.