扩展事件字符串中的 ClientID

发布于 2024-08-18 08:52:59 字数 1054 浏览 3 评论 0原文

我遇到了 <%= obj.ClientID %> 问题扩展,在 .ascx 用户控件中。

我有一个 .js 文件,包含一个 javascript 函数:

function doSomething(objectId)
{
    ...
}

我有一个 .ascx 文件,其中包含一些 html 元素,并且在一个元素的 onclick= 中我想调用 doSomething(),传递该 .ascx 文件中元素的 ID,其中传递的 ID 是所单击的元素以外的元素的 ID,因此我不能使用“this.”。

也许举个例子会更清楚。

这有效:

<script type="text/javascript">
    function redirect()
    {
        doSomething('<%= top.ClientID %>');
    }
</script>
<div id="top" runat="server">
    <img src="..." alt="..." onclick="redirect();"/>
</div>

但这不起作用:

<div id="top" runat="server">
    <img src="..." alt="..." onclick="doSomething('<%= top.ClientID %>');"/>
</div>

当我查看源代码时,我发现 <%= %>替换尚未发生,而不是“doSomething('ctl00_myControl_top');”我得到“doSomething('<%= top.ClientID %>');”

由于某种原因,脚本扩展发生在前一种情况下,但不会发生在后者中。当然,这种解决方法是不可接受的,因为如果我在页面上包含该控件的多个副本,它将中断 - 只有一个实例的“redirect()”函数可以访问。

关于如何使这种替代起作用有什么想法吗?

I'm having a problem with <%= obj.ClientID %> expansion, in a .ascx user control.

I have a .js file, containing a javascript function:

function doSomething(objectId)
{
    ...
}

I have a .ascx file, with some html elements, and in one element's onclick= I want to call doSomething(), passing the ID of an element in that .ascx file, where the passed ID is of an element other than the one being clicked on, so I can't use "this.".

Maybe it'd be clearer with an example.

This works:

<script type="text/javascript">
    function redirect()
    {
        doSomething('<%= top.ClientID %>');
    }
</script>
<div id="top" runat="server">
    <img src="..." alt="..." onclick="redirect();"/>
</div>

But this does not:

<div id="top" runat="server">
    <img src="..." alt="..." onclick="doSomething('<%= top.ClientID %>');"/>
</div>

When I look at the source, I see that the <%= %> substitution has not happened, instead of "doSomething('ctl00_myControl_top');" I get "doSomething('<%= top.ClientID %>');"

For some reason, the script expansion happens in the former case, but not in the latter. The work-around, of course, is not acceptable because it will break if I include multiple copies of the control on a page - only one instance's "redirect()" function will be accessible.

Any ideas on how to make this substitution work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-08-25 08:52:59

在我的机器上可以运行吗?

<div id="top" runat="server">
    <a href="#" onclick="doSomething('<%= top.ClientID %>')">rarrarara</a>
</div>

成为

<div id="ctl00_ContentPlaceHolder1_top">
    <a href="#" onclick="doSomething('ctl00_ContentPlaceHolder1_top')">rarrarara</a>
</div>

Works on my machine?

<div id="top" runat="server">
    <a href="#" onclick="doSomething('<%= top.ClientID %>')">rarrarara</a>
</div>

Becomes

<div id="ctl00_ContentPlaceHolder1_top">
    <a href="#" onclick="doSomething('ctl00_ContentPlaceHolder1_top')">rarrarara</a>
</div>
帅气尐潴 2024-08-25 08:52:59

考虑替代路线

确保您在 JavaScript 中使用内联表达式<%= (controlName).ClientID %> 引用的控件具有其“ClientIDMode”说明符设置为静态值,然后只需使用该控件的 ID 字段中的文本来引用它。我最终在最近的一个项目中使用了它,它效果很好。请参阅下面的链接以获取更详细的说明:

代码项目 - ASP.NET v4.0 客户端 ID 功能

除了将“ClientIDMode”说明符设置为“静态”之外,我还发现了一个关于从全局资源文件放置文本的有用想法(在语言切换的情况下)到不必在服务器级别运行的标准 HTML 控件的字段中。我在一个标准 HTML 按钮上使用了它,该按钮应该调用一个 JavaScript 函数来显示/隐藏特定的 div 或 ASP 面板。在内联表达式标签中使用 GlobalResource 函数,如下所示:

<input id="btnToggleFilterOptions" type="button" value="<%=GetGlobalResourceObject("SiteResource", "btnToggleFilterOption")%>" onclick="javascript:ToggleCssClass('divFilterOption','visible'); return false;" class="button submit" />

<asp:Panel ID="divFilterOption" ClientIDMode="Static" runat="server">
    <asp:TextBox ID="txtFilterOption1" runat="server" />
</asp:Panel>

我知道这是一篇旧文章,但在 Google 上搜索 Asp .NET 和 ClientID 时它很受欢迎。我希望这可以帮助其他人!

Consider an alternate route:

Ensure that the control you are referencing in JavaScript using the inline expression <%= (controlName).ClientID %> has its 'ClientIDMode' specifier set to a static value and then simply use the text in the ID field of that control to refer to it. I ended up using this in a recent project it works great. Please see the link below for a more detailed explanation:

Code Project - ASP.NET v4.0 Client ID Feature

Along with setting the 'ClientIDMode' specifier to "Static", I found a useful idea regarding placing text from a global resource file (in the case of language switching) into a field of a standard HTML control that does not have to run at the server level. I used this on a standard HTML button that was supposed to call a JavaScript function that would show/hide a specific div or ASP Panel. Use the GlobalResource function in the inline expression tags like so:

<input id="btnToggleFilterOptions" type="button" value="<%=GetGlobalResourceObject("SiteResource", "btnToggleFilterOption")%>" onclick="javascript:ToggleCssClass('divFilterOption','visible'); return false;" class="button submit" />

<asp:Panel ID="divFilterOption" ClientIDMode="Static" runat="server">
    <asp:TextBox ID="txtFilterOption1" runat="server" />
</asp:Panel>

I know this is an old post, but it is a popular hit when searching on Google for Asp .NET and ClientID. I hope this helps somebody else out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文