ASP.NET:是否可以在不使用.NET 代码(即VB.NET 或C#)的情况下创建动态命名的Javascript 函数?
我有一个自定义用户控件 (ascx),其中包含一个文本框和一个基于 Javascript 的计数器,让用户知道他们还需要输入多少字符。该控件中的内容如下:
function GetTextBox() {
return document.getElementById("<%=txNotes.ClientID %>");
}
当我们在页面上只有该用户控件的一个实例时,这种方法工作得很好,但现在我们必须支持多个实例。如您所知,页面上有此控件的多个实例将导致多个 GetTextBox() 函数,无论如何都只会调用最后一个函数。为了支持多个实例,我使用这个:
if (!string.IsNullOrEmpty(TextBoxName) && !Page.ClientScript.IsClientScriptBlockRegistered(TextBoxName))
{
string Script = string.Format("function Get{0}Notes() {{ return document.getElementById(\"{1}\"); }}",
TextBoxName, txNotes.ClientID);
Page.ClientScript.RegisterClientScriptBlock(GetType(), TextBoxName, Script, true);
}
TextBoxName
是一个公共用户控件属性,因此如果开发人员传递 Employee,它将生成一个名为 GetEmployeeNotes()
的 Javascript 函数。这非常有效,因为现在我们可以拥有一个独特的 GetNotes() 函数。
但是,我不喜欢它是如何硬编码到代码隐藏中的。我想要一个基于标记的解决方案,如果我想更改 Javascript,不需要重建项目。有谁知道有什么方法可以做到这一点?
编辑:我已经考虑过创建一个可以用文本阅读器阅读的单独的 .js 文件,但这听起来有点老套,如果可能的话,我想避免这种情况。
编辑2:Guard的下面的答案是可行的,但我不想走那条路,因为我在他的答案下面给出了原因。如果没有人可以提供另一种方法来完成我想做的事情,我很可能会将他的方法标记为答案,因为它在技术上完全符合我的要求。
I have a custom user control (ascx) that contains a textbox and a Javascript-based counter to let the user know many characters they have left to type. In this control is the following:
function GetTextBox() {
return document.getElementById("<%=txNotes.ClientID %>");
}
This worked fine when we only had one instance of this user control on the page, but now we have to support multiple. As you know, having multiple instances of this control on a page will result in multiple GetTextBox() functions, only the last of which will be called no matter what. To support multiple instances, I use this:
if (!string.IsNullOrEmpty(TextBoxName) && !Page.ClientScript.IsClientScriptBlockRegistered(TextBoxName))
{
string Script = string.Format("function Get{0}Notes() {{ return document.getElementById(\"{1}\"); }}",
TextBoxName, txNotes.ClientID);
Page.ClientScript.RegisterClientScriptBlock(GetType(), TextBoxName, Script, true);
}
TextBoxName
is a public usercontrol property, so if the developer passes Employee through, it will generate a Javascript function called GetEmployeeNotes()
. This works greate because now we can have a unique GetNotes() function.
However, I don't like how it's hardcoded into the codebehind. I would like a markup-based solution for this, something that doesn't require a rebuild of the project in case I want to change the Javascript. Does anyone know of a way to do this?
Edit: I've already thought of creating a separate .js file that I could read with a text reader, but that sounds a bit hacky and I'd like to avoid that if at all possible.
Edit 2: Guard's answer below would work, but I don't want to go that route for the reason I gave beneath his answer. If no one can offer another way to do what I want to do, I will most likely mark his as the answer since it technically does exactly what I am asking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 .NET 专家,但它不是作为预处理器工作的吗?
写作不合法吗
?
I'm not a .NET specialist, but isn't it working as a preprocessor?
Isn't it legal to write
?
为什么不使用通用函数并只传递相应文本框的 id 呢?如:GetNotes(thisTextBoxId) {...}。这不仅可以解决您的问题,而且更加干。
Why not use a generic function and just pass the id of the corresponding textbox? As in: GetNotes(thisTextBoxId) {...}. Not only would that deal with your problem but also is more DRY.