your markup need to change little(single quote/double quote combination). Carefully follow the following line and find out what did you miss.
<asp:CheckBox ID="chkSelect" onchange=<%# "javascript:checkUncheck('" + Eval("ContactName") + "')" %> Text='<%#Eval("ContactName") %>' runat="server" />
Markup:
<asp:gridview runat="server" id="grdData" autogeneratecolumns="False" xmlns:asp="#unknown">
<columns>
<asp:boundfield runat="server" showheader="True" headertext="Id" datafield="Id" />
<asp:templatefield headertext="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" onchange=<%# "javascript:checkUncheck('" + Eval("ContactName") + "')" %> Text='<%#Eval("ContactName") %>' runat="server" />
</ItemTemplate>
</asp:templatefield>
</columns>
</asp:gridview>
Data Model:
public class Data
{
public long Id { get; set; }
public string ContactName { get; set; }
public static IEnumerable<data> GetDataList()
{
return new[] { new Data { Id = 1, ContactName = "Habib" }, new Data { Id = 2, ContactName = "Khorshed" }, new Data { Id = 3, ContactName = "Shojol" } };
}
}</data>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
LoadGrid();
}
private void LoadGrid()
{
IEnumerable<data> data = Data.GetDataList();
grdData.DataSource = data;
grdData.DataBind();
}</data>
with asp .net checkbox onchange event will not work. If you see the render html
<span önchange="javascript:checkUncheck('Shojol')"><input id="MainContent_grdData_chkSelect_2" type="checkbox" name="ctl00$MainContent$grdData$ctl04$chkSelect" /><label for="MainContent_grdData_chkSelect_2">Shojol</label></span>
above you will understand that it will render a span and onchange event move from checkbox to span. In that case my suggestion is you can use onclick event instead of onchange and it will work fine.
You can RND with that like you can create 2 controls one server side another html and bind javascript method for onchange event handler for both and see the result.
<div>
<asp:checkbox id="chkTest" runat="server" onchange="javascript:checkUncheck('Good Morning');" text="Click Me" />
<input type="checkbox" id="chk3" onchange="javascript:checkUncheck('Good Morning');" value="Good Night"/>
</div>
<script type="text/javascript">
function checkUncheck(contactName) {
alert(contactName);
}
</script>