2011-07-07 50 views
0

嗨,我只是使用下面的代碼,並希望使一個按鈕可見和不可見基於複選框的狀態。我使用觸發器來調用一個事件,我將編寫代碼使該按鈕可見或不可見。如果我使用下面的代碼,我得到一個錯誤,如「System.InvalidOperationException:無法爲UpdatePanel UpdatePanel1中的觸發器找到帶有ID'chkDelete的控件'。」請幫幫我。asp.net觸發一個複選框控件

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers ="false"> 
    <ContentTemplate> 
      <asp:GridView ID="gvEventMechanic" runat="server" AutoGenerateColumns="False" PageSize="5" 
         GridLines="None" AllowSorting="true" BorderWidth="1" 
         EnableViewState="true" AllowPaging="true">  
      <Columns> 
       <asp:TemplateField>      
        <HeaderTemplate> 
         Disable 
         </HeaderTemplate> 
        <ItemStyle HorizontalAlign="Center" /> 
        <ItemTemplate>        
         <asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="true" ></asp:CheckBox> 
        </ItemTemplate> 
       </asp:TemplateField>      
      </Columns>     
     </asp:GridView> 
     </ContentTemplate> 
     <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="chkDelete" EventName="CheckBoxEventChanged" /> 
      </Triggers>      
     </asp:UpdatePanel> 

回答

0

控件ID爲CheckBox場<asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="true" ></asp:CheckBox>會爲這就是爲什麼它不能控件ID映射到觸發各行不同。
我建議你使用複選框的CheckedChanged事件來觸發你的方法。

+0

如果我使用CheckedChanged,那麼當單擊多個複選框時,頁面會回傳。爲了避免我要更新面板和觸發器。如果有其他方式,請讓我知道。僅供參考,複選框位於網格內。 – premg

+0

獲得控制ID很困難。也許你可以嘗試從客戶端使用JavaScript的東西。 – Saanch

+0

你能給我舉個例子。 – premg

1

作爲一種替代方案,爲什麼不使用客戶端環境來做到這一點?這更簡單,更原生。現在

$('#input[type=checkbox][id*=chkDelete]').change(function(){ 
     $('#button').toggleClass('disabled'); 
    }); 

,基於這個類,你可以使用CSS來調暗你的按鈕,如果它是一個跨度,或者一個div(自定義按鈕)。否則,您可以使用:

$('#input[type=checkbox][id*=chkDelete]').change(function(){ 
     if ($(this).is(':checked')) 
     { 
      $('#button').removeAttr('disabled'); 
     } 
     else 
     { 
      $('#button').attr('disabled', 'disabled'); 
     } 
    }); 
+0

我通過修改這個標籤得到了解決方案,但不知道是否正確觸發。 – premg

1

這會讓您獲得所有需要刪除相應記錄的信息。

// If you bind a list of objects as your data source you can use this to get the 
// index into the list. 
protected void OnCheckedChanged(Object sender, EventArgs e) 
{ 
    if (sender is CheckBox) 
    { 
     // we do this to get the index into the list of the item we want to work with. 
     CheckBox  cb = sender as CheckBox; 
     GridViewRow gvr = cb.NamingContainer as GridViewRow; 

     int dataItemIndex = gvr.DataItemIndex; // index into your list, regardless of page 
     int rowIndex  = gvr.RowIndex;  // row index in gridview. 
    } 
} 
0

是的,它是CheckedChanged。 雖然它沒有看到你在複選框中提到的東西。但它是這樣工作的。

相關問題