Loading multiple webusercontrol and javascript, ClientId is same error! RRS feed

  • Question

  • Hi!

    I'm rather new att webb development but so far I have made simple webusercontrol looking like this:

    <script language="JavaScript" type="text/javascript">
           function ExpandCollapse(block, button) {
               var tbl = document.getElementById(block);
               var btn = document.getElementById(button);
               if (tbl.style.display == "none") {
                   btn.src = "images/minus.jpg";
                   tbl.style.display = "block";
               else {
                   btn.src = "images/plus.jpg";
                   tbl.style.display = "none";
    <div id="Header" class="header">
        <img src="images/thirdparty.png" align="absmiddle" style="padding-left: 5" />
        <asp:Label ID="lbl_GroupName" runat="server" Text="[GroupName]"></asp:Label><a href="javascript:ExpandCollapse('datafield', 'btn_Apps')"><img
                src="images/minus.jpg" id="btn_Apps" /></a>
    <div id="dataField" style="overflow: hidden; display: block">
        <asp:GridView ID="DG_Items" runat="server" CssClass="mainTable">
            <AlternatingRowStyle BackColor="White" />

    Now on main main.aspx.cs file i load up till 10 of these webusercontrol dynamically. The problem is that all of them ends up with the same id for the div id=dataField and the a href  id=btn_Apps so when i click one of them the javascript is only executed for the first control.

    This is due to that all id:s in the usercontrols are the same for all usercontrols. So how do i approach this. How can the javascript tell wich of them i click?

    • Moved by Mike Dos Zhang Wednesday, August 22, 2012 3:22 AM (From:JScript for the .NET Framework)
    Friday, August 17, 2012 5:08 AM

All replies

  • Hi Tobias.K

    Welcome to MSDN Forum .

    This forum  supports  JScript .NET  not JavaScript .

    So , for JavaScript  related questions , please refer to : http://forums.asp.net/130.aspx .

    You may have more luck getting  answers  there and this issue will be moved to off-topic  forum .

    Thanks for your support and  understanding !

    Regards ,

    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 20, 2012 6:40 AM