6

I need to pass div client id to JavaScript of a repeater

I have 3 divs inside a repeater i have onmouseover event i want to grab client id of div element Is there any way i can pass exact client of div element

Can u guys help me out Thanks

jhon
  • 63
  • 1
  • 1
  • 3

4 Answers4

9

If you would like to do it in markup, you can use the following to get the ClientId:

<%# Container.FindControl("_RepeaterEL").ClientID %>
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
Calciol
  • 429
  • 4
  • 5
6

Something like this (if I understood you correctly):

Markup:

<asp:Repeater id="myRepeater" OnItemDataBound="myRepeater_ItemDataBound" runat="server">
    <ItemTemplate>
        <div id="myDiv" runat="server">......</div>
    </ItemTemplate>
</asp:Repeater>

Code-behind:

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HtmlGenericControl myDiv = e.Item.FindControl("myDiv") as HtmlGenericControl;
        // you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element
        myDiv.Attributes.Add("onmouseover", "doStuff('" + myDiv.ClientID + "');");
    }
}
volpav
  • 5,090
  • 19
  • 27
2
<asp:Panel CssClass="modal hide fade" ID="myModal" runat="server">
                            <div class="modal-header">
                                <a class="close" data-dismiss="modal">×</a>
                                <h3>Add to cart</h3>
                            </div>
                            <div class="modal-body">
                                <nav>
                                    <iframe seamless src="/ToCart/<%# DataBinder.Eval(Container.DataItem, "code")%>"
                                        style="border-style: none;"> </iframe>
                                </nav>
                            </div>
</asp:Panel>

<a data-toggle="modal" href="#<%#Container.FindControl("myModal").ClientID%>">
    <div class="add-to-cart-one">+</div>
</a>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
0

If the repeater renders to the browser, you can get the repeater element with:

var rep = $get("<%= rpt.ClientID %>");

Otherwise, wrap a

<DIV id="RepeaterEL"></div> 

around the repeater, and access its children either through pure JavaScript (and the childNodes collection), or using JQuery like

$("#RepeaterEL").children("DIV").each(function(i) {    
   var id = $(this).attr("id"); //<- pointer to DIV });
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • http://stackoverflow.com/questions/2567040/asp-controls-id-generation-inside-repeater i have same problem is there any way i can pass client id to javascript – jhon Jan 25 '11 at 17:08