2011-06-28 26 views
1

有誰知道這是怎麼回事?我有下面的代碼在我的aspx文件:無法獲得asp:在.net c jquery的複選框ID#

<tr> 
    <th class="graytext r">Add Reps to Team:</th> 
    <td>   
    <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="dsEmployees" EnableViewState="false" GridLines="None" CssClass="clGridDirectory"> 
      <Columns> 
       <asp:TemplateField > 
       <ItemTemplate> 
        <asp:CheckBox runat="server" ID="employee_name" Text='<%# Eval("fullname") %>'/> 
        <asp:HiddenField runat="server" ID="employeeidToRep" Value='<%# Eval("employeeid") %>'/> 
        <asp:TextBox runat="server" ID="repID" Text='<%# Eval("rep_id") %>' CssClass="cl_required_for_sale" data-messages="{required:'required'}" /> 
        <asp:RequiredFieldValidator ID="reqvrepID" runat="server" ControlToValidate="repID" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> 
       </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
      </asp:GridView> 
     <asp:SqlDataSource ID="dsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="app_staff_without_team_select" SelectCommandType="StoredProcedure"> 
     </asp:SqlDataSource>   
    </td> 
</tr> 

我試圖得到一個警報一旦用戶的複選框點擊使用下面的代碼:

<script type="text/javascript"> 
    $('#<%= employee_name.ClientID %>').change(function() { 
     alert('bingo') 
    }); 
</script> 

但不知何故錯誤消息顯示時我試圖運行該頁面:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'employee_name' does not exist in the current context 

Source Error:  

Line 121: <script type="text/javascript"> 
Line 122:  $('#<%= employee_name.ClientID %>').click(function() { 

有誰知道我的jQuery代碼出了什麼問題?我怎麼才能得到jQuery的複選框ID?

+0

您如何嘗試在'document.ready'部分添加此項? – V4Vendetta

回答

0

將函數附加到複選框單擊窗體後面的代碼,即RowDataBound事件。而不是直接做它不會那樣工作。

你的JavaScript就像

protected void GridView1_RowDataBound(object sender, 
        GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox checkBox = (CheckBox) row.FindControl("FordelingCheckBox"); 

      checkBox.Attributes.Add("onclick","javascript:alert('clicked')"); 
     } 
    } 
1

而不是使用CheckBox的ID(這是不適合的,無論如何模板內),請使用Checkbox.CssClass,如:

CssClass="employee_name" 

然後,在jQuery中,選擇器是'.employee_name',例如:

$('.employee_name').click(function() { alert('id: ' + this.id); }); 
1

您也可以使用

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:CheckBox onclick="javascript:alert('clicked')" runat="server" ID="employee_name" Text='<%# Eval("fullname") %>' />      
    </ItemTemplate> 
</asp:TemplateField> 

添加JS功能上onclick event of CheckBox

0

我更喜歡這種方式。下面的jQuery選擇器會將事件處理程序添加到ID以'employee_name'結尾的所有複選框。

<script type="text/javascript"> 
     $(document).ready(function() {    
      $(':checkbox[id$="employee_name"]').change(function() { 
       alert('bingo'); 
      }); 
     }); 
</script> 
+0

使用類選擇器甚至更好,因爲在網格視圖中,每個ID都會在運行時添加像employee_name1,employee_name2等數字後綴。 – Santoo

0

因爲複選框是在重複數據(轉發器)的由網格視圖中產生的複選框的ID是動態的,而不是靜態的控制(第一行將具有取決於ID = employee_name_0或不同在clientIdMode屬性上)。

爲了將事件處理程序添加到複選框中,您需要以不同的方式選擇複選框,或者通過css類或其他方法來選擇複選框。

附加說明:如果要使用項目模板中的其他控件,則無法通過ID選擇它們,原因相同,因此無法通過ID選擇複選框。爲此,您需要將CSS類添加到所需的控件中,並添加一個選擇器,以選擇具有特定類的複選框controll的兄弟。