1

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?

DreamTeK
  • 32,537
  • 27
  • 112
  • 171
Ram Singh
  • 6,664
  • 35
  • 100
  • 166

1 Answers1

6

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 );
    }
}
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • thanks sir. I have lost 12 points to get this answer. lolz.. thanks once again. – Ram Singh Nov 01 '11 at 10:07
  • How can this one work? FindControl only return 1 control. And we should not use the same ID for multiple elements. Instead we can set HTML attribute directly: onmouseover="<% ... %>" – Hp93 Jan 02 '16 at 15:11