How to call JavaScript/jQuery function from .cs file (C#)

前端 未结 3 1059
情深已故
情深已故 2021-01-15 02:01

I\'m working on asp.net (C#) project which include some page files (aspx, aspx.cs) and some only (.cs) file. I\'m able to access JavaScript/jQuery functions from page files

相关标签:
3条回答
  • 2021-01-15 02:42

    you can try to write to a response stream directly and output

    Response.Write("<script type=\"text/javascript\"> your JS call</script>");
    

    Regards, Szymon

    0 讨论(0)
  • 2021-01-15 02:45

    You can call a JavaScript function using registerStartupScript.

    ASPX Page

    <script type="text/javascript">
    
       function printOutput(arg) {            
          alert(arg);
       }
    
    </script>
    

    Code Behind

    protected void DoSomethingButton_Click(object sender, EventArgs e)
    {
       ClientScriptManager cs = Page.ClientScript;
    
       cs.RegisterStartupScript(typeof (Page), "PrintScript_" + UniqueID, "printOutput('My HTML Code Generated by jquery function');", true);
    }
    
    0 讨论(0)
  • 2021-01-15 02:48

    You need to pass into the web.cs a reference to the page. You will then have access to the page.clientscript method

    0 讨论(0)
提交回复
热议问题