Richard Protzel Richard Protzel - 1 year ago 69
ASP.NET (C#) Question

jquery example in is not working

I am trying out the following jquery/ code sample in Visual Studio 2013. Unlike the example below -- in my exercise I have placed the c# code in the code behind page and I placed the jquery code with the aspx markup. The gridview displays the data, but when I click on the button the alert is not invoked. When I try setting debugging on the jquery, the jquery code is not being entered. I would be grateful if someone could point out what I am missing:


protected void Page_Load(object sender, EventArgs e)

var myTestList = new List<MyTestClass1>
new MyTestClass1 {ID = 1, Name = "Name1"},
new MyTestClass1 {ID = 2, Name = "Name2"},
new MyTestClass1 {ID = 3, Name = "Name3"},
new MyTestClass1 {ID = 4, Name = "Name4"},
new MyTestClass1 {ID = 5, Name = "Name5"},
new MyTestClass1 {ID = 6, Name = "Name6"}

GridView1.DataSource = myTestList;

public class MyTestClass1
public int ID { get; set; }
public string Name { get; set; }



<%@ Page Language="C#" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="WebApplication1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">

//--I'm doing this in code behind

<html xmlns="">
<head runat="server">
<script type="text/javascript" src=""></script>
<form id="HtmlForm" runat="server">
<asp:GridView ID="GridView1" runat="server"
<asp:CheckBox ID="CheckSelect" runat="server" />
<asp:HiddenField ID="hdID" runat="server"
Value='<%# Eval("ID") %>' />
<asp:Label runat="server" Text='<%# Eval("Name") %>'>
<br />
<asp:Button ID="btnGetData" runat="server" Text="Get Data" />


var gridView1Control = document.getElementById('<%= GridView1.ClientID %>');

$('#<%= btnGetData.ClientID %>').click(function(e) {
//To uncheck the header checkbox when there are no selected checkboxes in itemtemplate
$('input:checkbox[id$=CheckSelect]:checked', gridView1Control).each(function(item, index) {

var id = $(this).next('input:hidden[id$=hdID]').val();
return false;


Answer Source

Its because the element CheckSelect inside the asp:GridView GridView1 gets the ID GridView1_ChkSelect_{row_index} and hdID gets the id GridView1_hdID_{row_index}. Here you are using the selector $= which

Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.

What you need is *=

Selects elements that have the specified attribute with a value containing a given substring.

Change your code like this.

    gridView1Control).each(function (item, index) {

    var id = $(this).next('input:hidden[id*=hdID]').val();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download