2011-12-19 148 views
0

我有以下中继器控件,它绑定到DashboardPage对象的集合。 DashboardPage具有以下属性:名称,页码将控件的ID属性绑定到对象属性

<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> 
     &nbsp;&nbsp; 
     </SeparatorTemplate> 
    </asp:Repeater> 

我想的ImageButton的ID属性绑定到命名DashboardPage对象,像这样

ID="<%# Eval('Name') %>" 

的财产,但抛出一个异常:

控件的ID属性可以只能使用标签中的ID属性和一个简单的值进行设置。

我需要图像的ID属性,因为我有一个客户端脚本,它使用它的ID来更改图像。有没有解决的办法?

感谢答复

问候 加吉克Kyurkchyan

+0

那为什么你没有ID = YourIMageID您的图像按钮 – BizApps 2011-12-19 08:22:40

+0

为什么不使用的CssClass或类或自定义属性而不是ID,注意:如果您在中继器项目上使用ID,则ID将以容器的ID加上下划线作为前缀,具体取决于级别 – jerjer 2011-12-19 08:28:49

+0

而不是使用usin g数据绑定#,你有没有试过使用=? <%= Eval('Name')%> – TheGeekYouNeed 2011-12-19 08:46:06

回答

1

既然你已经找到了你不能动态设置你需要找到使用JavaScript参考图像的另一种方式的服务器控件的ID。有三种方法我可以想到:

  1. 设置控件的CssClass属性并改为使用它。使用DOM来查找类的控件而不是使用ID的控件效率稍低,但在现实世界中可能忽略不计。

  2. 将图像包装在标准DIV中。并在容器DIV上设置ID。然后,您可以使用类似的jQuery去$("#divid img")...

  3. 使用标准<img>标签(无runat="server"),而不是asp:Image控制。然后您可以毫无问题地设置ID。

+0

感谢您的回答! – kyurkchyan 2011-12-19 11:09:31

0

您可以使用ItemDataBound来设置ID。

代码隐藏:

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    var textbox = e.Item.FindControl("myTextBox"); 
    textbox.ID = "myTextBox" + (e.name).toString(); /*ToString() method can be unnecessary in some cases.*/ 
} 

中继器:

<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="BehaviorCustomizeRepeater_ItemDataBound"> 
/*other stuffs*/ 
</asp:Repeater> 
相关问题