2010-11-30 37 views
0

我下面的帖子:多用戶控制和客戶端ID asp.net JS

http://mosesofegypt.net/post/GridView-with-Select-All-CheckBox-using-JQuery.aspx

建立在asp.net GridView的複選框列與選擇/取消選擇所有功能。 就我而言,gridview和javascript(JQuery)代碼位於動態加載用戶控件的內部,並且該用戶控件有多個實例。選擇/取消選擇功能僅在頁面上創建的最後一個用戶控件中起作用。 換句話說,js腳本中的gvProducts.ClientId只知道最後一個gridview。 任何想法如何才能在其他用戶控件(即除最後一個)之外的GridView的ClientIds引用? 任何幫助將不勝感激。

編輯:這個問題基本上和在這篇文章中提到的一樣:Multiple user control instances and ClientID in JS 但是沒有幫助的答案。

感謝, 阿里

回答

0

你可以添加一個的CssClass到GridView和基本的jQuery選擇過這一點。

腳本:

<script type="text/javascript"> 
    $(function() { 
     $(".t th input:checkbox").click(function() { 
      var $checkbox = $(this).closest('table').find(':checkbox:not(:first)'); 
      $checkbox.attr('checked', $(this).attr('checked')); 
     }); 
    }); 
</script> 

2表具有相同的類名:

<table class="t">   
    <tr> 
     <th align="left" scope="col">Category</th> 
     <th align="left" scope="col">Product</th> 
     <th align="right" scope="col">Unit Price</th> 
     <th align="left" scope="col">Supplier</th> 
     <th scope="col"> 
      <input type="checkbox" name="select-all" /> 
     </th> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
</table> 
<br /><br /><br /> 
<table class="t">   
    <tr> 
     <th align="left" scope="col">Category</th> 
     <th align="left" scope="col">Product</th> 
     <th align="right" scope="col">Unit Price</th> 
     <th align="left" scope="col">Supplier</th> 
     <th scope="col"> 
      <input type="checkbox" name="select-all" /> 
     </th> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
</table> 
+0

感謝fehays,但這樣CssClass屬性將適用於網頁上的所有GridView的和選擇/取消選擇所有的意志適用於所有的gridviews。我需要能夠在單個gridview上工作。還有什麼建議? – Ali 2010-11-30 16:58:47