I’m trying to run a client-side script from the server-side on Page_Load, I found this code from here.
It’s server side code:
protected void Page_Load(object sender, EventArgs e)
{
if (!ClientScript.IsStartupScriptRegistered("alert"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"alert", "alertMe();", true);
}
}
And it’s client side code:
<script type="text/javascript">
function alertMe() {
alert('Hello');
}
</script>
But nothing happens on Page_Load.
UPDATE: source code coderun.
You are calling your
alertMe()before it is defined, at the end of the page.All scripts registered with asp.net are rendered in the form tag.
I suggest moving your javascript block to the head of the page.
Here is your modified code run that works as you expect.