2016-11-19 104 views
0

我有以下的Javascript功能來複制選中(使用複選框)行值從Gridview1到GridView2。如何添加選擇和GridView的刪除按鈕使用JavaScript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     var temprow = $('[id*=GridView2] table tbody').find(".emptyTd"); 

     $('[id*=cbCheck]').change(function() { 
      var checkbox = $(this); 
      if ($(this).is(":checked")) { 
       var row = $(this).parent().closest('tr'); 
       var temp = $(row).clone(true); 
       $('[id*=GridView1] tbody').append(temp); 
       $('[id*=GridView2] table tbody').find(".emptyTd").remove(); 
       $(row).find("td:first").remove(); 
       $('[id*=GridView2] table tbody').append(row); 
      } 
      else { 
       $('[id*=GridView2] table tbody tr').each(function() { 
        if ($(this).find("td:first").html() == $(checkbox).parent().closest('tr').find("td:nth-child(2)").html()) { 
         $(this).remove(); 
         if ($('[id*=GridView2] table tbody').has('td').length == 0) { 
          $('[id*=GridView2] table tbody').append(temprow); 
         } 
        } 
       }); 
      } 
     }); 
    });  
</script> 

現在我想添加使用Javascript/jQuery的在GridView2 SELECTDELETE按鈕。

我GidView2樣子:

<asp:GridView ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" 
    runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="GridView2_SelectedIndexChanged"> 
<EmptyDataTemplate> 
     <table border="0" cellpadding="0" cellspacing="0" width="325px"> 
      <tr style="color: White; background-color: #3AC0F2;"> 

       <th scope="col"> 
        Item Code 
       </th> 
       <th scope="col"> 
        Item Name 
       </th> 
       <th scope="col"> 
        Unit 
       </th> 
       <th scope="col"> 
        Price 
       </th> 
       <th scope="col"> 
        Stock 
       </th> 
      </tr> 
      <tr class="emptyTd"> 
       <td style="width: 30px; text-align: center" colspan="3"> 
        No Records found 
       </td> 
      </tr> 
     </table> 
    </EmptyDataTemplate> 
</asp:GridView> 
+0

你打算用這些按鈕做什麼?使用Javascript創建的內容將在頁面刷新/回傳時丟失。他們將沒有與asp.net相關的功能。 – VDWWD

回答

0

我不建議添加控件在客戶端使用它作爲服務器控件。

我推薦用的CssClass隱藏它們添加在服務器端的按鈕。 而只是使用相同的CSS類與jQuery來顯示。

+0

我想,如果我在服務器端添加按鈕,數據將丟失頁面刷新/回發。 – Ram

+0

如果您在頁面加載後數據綁定您的網格,會發生這種情況。嘗試在預加載或不回傳時執行此操作 – Arfilon

相關問題