How can I call javascript method from my aspx.cs file

我的未来我决定 提交于 2019-12-20 05:31:30

问题


I have a javascript method which I have to call from the aspx page at the time of page load.


回答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);
    }
}


来源:https://stackoverflow.com/questions/814642/how-can-i-call-javascript-method-from-my-aspx-cs-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!