如何在运行时将属性添加到转发器项目? [英] How to add an attribute to repeater item at runtime?

查看:79
本文介绍了如何在运行时将属性添加到转发器项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,我想将鼠标悬停在其项目上.

I have a repeater and i want to add a mouse over attribute to its items.

是否可以在运行时添加属性,如果可以,怎么办?

Is it possible to add attributes at run-time, if yes then how?

推荐答案

标记:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
     <HeaderTemplate>
          <table>
     </HeaderTemplate>
     <ItemTemplate>
          <tr runat="server" id="itemRow">
               <td>
                    <%# Container.DataItem.ToString() %>
               </td>
          </tr>
     </ItemTemplate>
     <FooterTemplate>
          </table>
     </FooterTemplate>
</asp:Repeater>

代码:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var row = e.Item.FindControl("itemRow") as HtmlTableRow;
    if (row != null)
    {
        row.Attributes["onmouseover"] = string.Format("alert('Hello from row #{0}');", e.Item.ItemIndex );
    }
}

这篇关于如何在运行时将属性添加到转发器项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆