How to add an attribute to repeater item at runtime?

大憨熊 提交于 2019-12-13 18:59:43

问题


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?


回答1:


Markup:

<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>

Code:

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 );
    }
}


来源:https://stackoverflow.com/questions/7963357/how-to-add-an-attribute-to-repeater-item-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!