绑定控件的ID属性为对象属性 [英] Bind control's ID property to object property
问题描述
我有以下的直放站监控,这势必DashboardPage对象的集合。 DashboardPage具有以下属性:命名,PAGENUMBER
I have the following repeater control, which is bound to collection of DashboardPage objects. DashboardPage has the following properties: Name, Pagenumber .
<asp:Repeater ID="reportPages" runat="server">
<ItemTemplate>
<asp:ImageButton runat="server" onclick="ImageButton_Click"
ImageUrl="<%# GetImagePath(Container.DataItem) %>"
onmouseover="<%# GetMouseOverEventString(Container.DataItem) %>"
onmouseout="<%# GetMouseOutEventString(Container.DataItem) %>"
/>
</ItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
</asp:Repeater>
我要的ImageButton的ID属性绑定到名称DashboardPage对象的属性,像这样
I want to bind ImageButton's ID property to Name property of DashboardPage object, something like this
ID="<%# Eval('Name') %>"
但会抛出异常:
控件的ID属性只能使用标签的ID属性和一个简单的值进行设置。
The ID property of a control can only be set using the ID attribute in the tag and a simple value.
我需要的图像的ID属性,因为我有一个客户端脚本这改变了形象,用它的ID。有没有办法解决?
I need the ID property of the image, because I have a client side script which changes the image, using the it's ID. Is there a way around this?
感谢您的答复。
问候
加吉克Kyurkchyan
Regards Gagik Kyurkchyan
推荐答案
既然你已经找到了你不能动态设置你需要找到使用JavaScript引用你的形象的另一种方式的服务器控件的ID。有三种方法我能想到的:
Since you have found you cannot dynamically set the ID of a server control you need to find an alternative way of referencing your image using JavaScript. There are three ways I can think of:
-
设置控件并使用它的CssClass属性。这是一个有点更低效找到使用DOM类,而不是ID的控制,但可能是在现实世界中可以忽略不计。
Set the CssClass property of the control and use that instead. It's a little more inefficient to find a control by class rather than ID using DOM, but that is probably negligible in the real world.
包装你的图像在一个标准的 DIV
。并设置容器 DIV
上的ID。然后,您可以使用类似的jQuery去 $(#DIVID IMG)...
Wrap your image in a standard DIV
. and set the ID on the container DIV
. You can then use something like jQuery to go $("#divid img")...
使用标准&LT; IMG&GT;
标签(不 =服务器
),而比 ASP:图像
控制。然后,您可以设置ID上,没有问题。
Use a standard <img>
tag (with no runat="server"
) rather than an asp:Image
control. You can then set the ID on that without issue.
这篇关于绑定控件的ID属性为对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!