Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am binding gridView in code behind. I want to display Contact Names using javascript depending on checked checkboxes.
<asp:TemplateField HeaderText="Select">
                            <asp:CheckBox ID="chkSelect" onchange='return checkUncheck(<%#Eval("ContactName") %>);'
                                Text='<%#Eval("ContactName") %>' runat="server" />

Eval Function is working in Text property and displaying contact names in checkboxes values. But in checkUncheck() it not working. In source of the page I can find this argument in checkUncheck() function:
onchange="return checkUncheck(&lt;%#Eval(&quot;ContactName&quot;) %>);"
Updated 13-Sep-17 0:52am

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" />

<asp:gridview runat="server" id="grdData" autogeneratecolumns="False" xmlns:asp="#unknown">
        <asp:boundfield runat="server" showheader="True" headertext="Id" datafield="Id" />
        <asp:templatefield headertext="Select">
                <asp:CheckBox ID="chkSelect" onchange=<%# "javascript:checkUncheck('" + Eval("ContactName") + "')" %> Text='<%#Eval("ContactName") %>' runat="server" />

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" } };

Code behind
protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
private void LoadGrid()
    IEnumerable<data> data = Data.GetDataList();
    grdData.DataSource = 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.

        <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"/>

<script  type="text/javascript">
    function checkUncheck(contactName) {
Share this answer
Try this way!!!
<asp:CheckBox ID="ChkAll" runat="server" Text='<%# Bind("ContactName") %>' OnClientClick='<%# string.Format("javascript:return checkUncheck(\"{0}\")", Eval("ContactName")) %>'>
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900