I have a javascript method which I have to call from the aspx page at the time of page load.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Javascript methods are client-side methods, so you can not call them in your server side code.
But if you’re looking for a way to call your method at the page load put your method call into script tag and write it in your aspx page :
<body>
<script language="javascript">
myMethod();
</script>
</body>
Or you can register your scripts from code-behind like that :
protected void Page_Load(object sender, EventArgs e)
{
string script = "myMethod();";
if (!this.Page.ClientScript.IsClientScriptBlockRegistered("myPostBackScript"))
{
this.Page.ClientScript.RegisterClientScriptBlock(typeof(MyPage),
"myPostBackScript", script, true);
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0