简体   繁体   中英

Call a javascript function from c# static method

I am using the below client script in aspx code behind to call a javascript function. But the below client script in public static method so i got the error in registerstartupscript first argument.My older post is here Call non-static function from static function

If any one have a possible solutions please post..

Page.ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "from_bill_tab();");

Try this:

ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", 
 "<script type='text/javascript'>alert('Error !!!');</script>");

I know this is an old question.I am answering this to help new users who come across this question.

To use clientscript inside a static method pass the Page object as a parameter to your static method

protected void Page_Load(object sender, EventArgs e)
        {
           LoadJavascript(Page);
        }



public static void LoadJavascript( Page page)
        {

           page.ClientScript.RegisterStartupScript(page.GetType(), "alert", "<script>alert('Hai');</script>");
        }

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