我如何判断
就像标题所说,我试图找出是否需要包含我的 ASP.net UserControl 需要工作的脚本库。我不想在每个页面多次包含它,但我希望我的控件能够在同一页面上多次使用。
如何在控件的代码隐藏中检查给定的 标记是否存在?
这是.Net 2.0,没有 LINQ。
Like the title says, I'm trying to find out if I need to include a script library that my ASP.net UserControl needs to work. I don't want to include it multiple times per page, but I want my control to be able to be used multiple times on the same page.
How can I, in the codebehind of my control, check to see if a given <script/>
tag is present?
This is .Net 2.0, no LINQ.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者如果您需要脚本块,并且想要包含控件的类型:
or if you need a script block, And want to include the control's type:
这对于您的使用来说可能有点大材小用,但我在添加到页面之前使用它来搜索现有的 CSS 和 JS:
如果您确定
LINK
或SCRIPT
保证位于HEAD
中。This might be a bit overkill for your usage, but I'm using it to search for existing CSS and JS before adding to the page:
It works great if you're sure that the
LINK
orSCRIPT
is guaranteed to be in theHEAD
.不,没有。
相反,您应该维护一组已包含的
标记,并在添加脚本时更新它。 (例如,
[ThreadStatic] static List
)No, there isn't.
Instead, you should maintain a set of
<script>
tags that have already been included, and update it whenever you add a script. (For example, a[ThreadStatic] static List<String>
)我建议重新考虑你的问题/解决方案。看起来您正在尝试做的事情是将您的用户控件和使用它的页面结合起来。这需要页面引用该脚本才能使用控件。
您是否有任何理由不能将脚本标记放入用户控件中,以便使用用户控件的页面不需要知道它们需要包含特定的脚本标记才能工作?
我倾向于认为,如果您只想使用一个控件,您应该能够直接使用它。
I would suggest re-thinking your problem/solution. It seems like what you are trying to do would couple your user control and the pages that use it. This would require that a page reference that script in order to use a control.
Is there any reason you can't just put the script tag in your user control so that the pages consuming your user control don't need to know that they need to include a specific script tag to work?
I tend to think that if you want to just use a control you should be able to just use it.