有谁知道这里发生了什么?我的aspx文件中有以下代码:
<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 :(得分:1)
使用Checkbox.CssClass
,而不是使用CheckBox的ID(无论如何都不适用于模板),例如:
CssClass="employee_name"
然后,在jQuery中,选择器是'.employee_name'
,例如:
$('.employee_name').click(function() { alert('id: ' + this.id); });
答案 1 :(得分:1)
您也可以使用
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox onclick="javascript:alert('clicked')" runat="server" ID="employee_name" Text='<%# Eval("fullname") %>' />
</ItemTemplate>
</asp:TemplateField>
在onclick event of CheckBox
答案 2 :(得分:0)
将功能附加到Checkbox单击表单后面的代码文件,即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')");
}
}
答案 3 :(得分:0)
我更喜欢这种方式。下面的jQuery选择器将向所有ID为“employee_name”的复选框添加事件处理程序。
<script type="text/javascript">
$(document).ready(function () {
$(':checkbox[id$="employee_name"]').change(function () {
alert('bingo');
});
});
</script>
答案 4 :(得分:0)
因为复选框位于重复数据(转发器)的控件中,所以网格视图生成的复选框的id是动态的而不是静态的(第一行将具有id = employee_name_0或不同,具体取决于clientIdMode属性)。
为了在复选框中添加事件处理程序,您需要以不同方式选择复选框,按css类或其他方式选择。
附加注意:如果要使用项目模板中的其他控件,则无法通过ID选择它们,因为您无法通过ID选中复选框。为此,您需要将css类添加到所需的控件并添加一个选择器,该选择器将选中具有特定类的复选框controll的兄弟节点。