简体   繁体   中英

How do I use RegisterClientScriptBlock to register JavaScript?

ASP.NET 2.0 provides the ClientScript.RegisterClientScriptBlock() method for registering JavaScript in an ASP.NET Page.

The issue I'm having is passing the script when it's located in another directory. Specifically, the following syntax does not work:

ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", "../dir/subdir/scriptName.js", true);

Instead of dropping the code into the page like this page says it should, it instead displays ../dir/subdir/script.js , my question is this:

Has anyone dealt with this before, and found a way to drop in the javascript in a separate file? Am I going about this the wrong way?

你所追求的是:

ClientScript.RegisterClientScriptInclude(this.GetType(), "scriptName", "../dir/subdir/scriptName.js")

use:ClientScript.RegisterClientScriptInclude(key,url);

您的脚本值必须是完整脚本,因此请为脚本值添加以下内容。

<script type='text/javascript' src='yourpathhere'></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