问题描述
我有一个页面,其具有一系列的MenuItems(从一个数据库中动态生成的)作为菜单的项上的项目。
I have a item on a page, which has a series of MenuItems (generated dynamically from a database) as Items of the Menu.
每个菜单项呈现自身作为
Each MenuItem renders itself as
<a class="ctl00_cphContent_cphMainContentTitle_uxHeaderMenu_menuPageNav_1 button ctl00_cphContent_cphMainContentTitle_uxHeaderMenu_menuPageNav_3" style="border-style:none;font-size:1em;" href="SomeURLHere.aspx">
不过,我想获得这个链接(我们使用外部JavaScript库弹出模态灯箱式的框架页)的客户端ID。其中一个这样的要求,我们需要确定可点击对象ID所以我们可以将它设置成能够开火单击事件。
However I would like to get the ClientID of this link (we use a external Javascript library to popup pages in modal lightbox style frames). One of the requirements of this is that we need to identify the "clickable object ID" so we can set it up to be able to fire the event on click.
在其他地方对我们的网站,我们有
Everywhere else on our site we have
OurSite.SetupDialogueBoxForOpen('<%= HyperLink.ClientID =>');
但由于某种原因,菜单项不出现有分配给它的ClientID属性。这使得设置几乎不可能在JavaScript中的客户端ID。
However for some reason the menu item does not appear to have a ClientID property assigned to it. Which makes setting the client id in JavaScript near impossible.
有谁知道如何获得一个菜单项的ClientID的(只是澄清菜单项的类型System.Web.UI.WebControls.MenuItem的
Does anyone know how to get a ClientID of a menuitem (just for clarification the menu item is of type System.Web.UI.WebControls.MenuItem
在此先感谢!
推荐答案
如果你想改变在控制方式的项目呈现,你可以使用 StaticMenuItemTemplate
和 DynamicMenuItemTemplate
属性。我只会用在我的例子第一(静态,对于顶级项目):
If you want to change the way items in the control are rendered, you can use the StaticMenuItemTemplate
and DynamicMenuItemTemplate
properties. I will only use the first in my example (static, for top-level items):
<asp:Menu runat="server" ...>
<StaticMenuItemTemplate>
<a id="<%# GetSuitableClientId(Container) %>"><%# Eval("Text") %></a>
</StaticMenuItemTemplate>
</asp:Menu>
的 StaticMenuItemTemplate
属性的类型的并经常与这样的模板属性的情况下,它装饰的 TemplateContainer
属性。这样就指定了该模板被实例化,通常容器可以访问某些情况下,你可能需要渲染的容器。在这种情况下,它是类型 MenuItemTemplateContainer
它实现 IDataContainer
,从而可以访问的数据项。
The StaticMenuItemTemplate
property is of type ITemplate
and as is often the case with such template properties it is decorated with the TemplateContainer
attribute. This specifies the container in which the template is to be instantiated, usually the container gives access to some context that you may need in rendering. In this case it is of type MenuItemTemplateContainer
which implements IDataContainer
and thus gives access to the data item.
因此,我们通过这个容器返回到我们的页面的方法,并在该方法中,我们构建一个ID,因为我们认为合适的。我们可以使用深度数据项,并将该容器的索引,例如:
So we pass this container back to a method in our page, and in that method we construct an ID as we see fit. We could use the data item for depth, and the container for the index for instance:
protected string GetSuitableClientId(MenuItemTemplateContainer container)
{
MenuItem item = (MenuItem)container.DataItem;
return String.Format("menuItem-{0}-{1}", item.Depth, container.ItemIndex);
}
我在建设这个答案的猜测是,你现在可以调整你的JavaScript绑定到点击事件&LT;一个ID =菜单项-2-4&GT;文字&lt; / A&GT;
元素,因为你现在有predictable客户端的ID。
My guess in constructing this answer is that you can now adjust your JavaScript to bind to click events on the <a id="menuItem-2-4">Text</a>
element since you now have predictable client-side IDs.
编辑:你也可以用你的模板内以下,让ASP.NET负责创建独特的客户端ID的照顾,但毕竟是少predictable ...
you can also use the following inside your template and let the ASP.NET take care of creating the unique client-side ID, but that is less predictable...
<asp:HyperLink ID="MenuItem" runat="server" NavigateUrl='<%# Eval("NavigateUrl") %>' />
这篇关于菜单项中的ClientID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!