简体   繁体   中英

Call Aspx java script function from ascx code behind file

I have a JavaScript function in aspx page and this aspx page had several ascx control in it.

I need to call that JavaScript function from one of its ascx control code behind file. I tried below approach but it is not working as expected. Any suggestion please.

in aspx page:

<script type="text/javascript">
    function Disable() 
    {
        // some code
        // return;
    }

in ascx code behind file:

ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "Script", "Disable();", True)

Can someone please let me know how to resolve this?

Try RegisterStartupScript instead of RegisterClientScriptBlock

ScriptManager.RegisterStartupScript(Page, GetType(), "Script", "Disable();", true);

RegisterClientScriptBlock writes the javascript content at the top of the HTML page content while RegisterStartupScript writes the content at the bottom. Chances are that your inline function 'Disable()` is below the code calling it, thus it does not found when fired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM