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">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" onchange='return checkUncheck(<%#Eval("ContactName") %>);'
                                Text='<%#Eval("ContactName") %>' runat="server" />
                        </ItemTemplate>
 </asp:TemplateField>


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;) %>);"
Posted
Updated 13-Sep-17 0:52am
v2

your markup need to change little(single quote/double quote combination). Carefully follow the following line and find out what did you miss.
ASP.NET
<asp:CheckBox ID="chkSelect" onchange=<%# "javascript:checkUncheck('" + Eval("ContactName") + "')" %> Text='<%#Eval("ContactName") %>' runat="server" />


Markup:
ASP.NET
<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:
C#
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
C#
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
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.NET
<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>

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