且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Page.ClientScript注册一个对JavaScript的code的

更新时间:2023-12-05 23:34:04

你是问,如果你的脚本会工作,或者你问这是否是将其添加到页面的好办法?

Are you asking if your script will work, or are you asking if this is a good way to add it to the page?

插入脚本的网页以这种方式肯定会工作,但***的做法会说你首先做一个检查:

Inserting the script in to the page in that way will certainly work, but best practice would say you do a check first:

if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "SmartPenCheck"))
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SmartPenCheck", strJS, false);

这样做它,虽然这样有点凌乱。无需重新编译应用程序,你不能代替脚本。你没有得到你会从一个单独的JavaScript文件有它的缓存。这是更难了很多调试。它是凌乱的cs文件阅读。

Doing it this way though is a little messy. You cannot replace the script without recompiling the app. You don't get the caching you would have from having it in a separate javascript file. It is a lot harder to debug. It is messy to read in the .cs file.

您要保存,当您需要插入动态值到JavaScript的(如控件ID)添加JavaScript的这个方法,或者您需要更改脚本是基于一些其他标准插入。

You want to save this method of adding javascript for when you need to insert dynamic values into the javascript (like control IDs), or you need to change which script is inserted based on some other criteria.