asp:checkboxfield javascript

I have a gridview control and within that I have asp:checkboxfield controls -- for one of the checkboxes when it is unchecked I need to popup a confirmation dialog box. Is there an easy way to accomplish this since the asp:checkboxfield does not seem to have an OnClientClick event?

                <EnhancedControls:CustomGridView ID="gvEmployers" runat="server" BackColor="White"
                    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
                    GridLines="Vertical" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True"
                    AutoGenerateDeleteButton="False" AutoGenerateEditButton="False" DataKeyNames="EmployerId">
                    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="Gainsboro" />
                        <asp:CommandField ButtonType="Image"
                                UpdateImageUrl="~/Includes/Images/statusicon_completed.gif" />
                        <asp:TemplateField HeaderText="Delete">
                                <asp:ImageButton  id="cmdDelete" CommandName="Delete" Text="Delete" runat="server" 
                                    imageurl="~/Includes/Images/delete.gif" tooltip="Delete" HeaderText="Delete"
                                    OnClientClick="return confirm('Are you sure you want to delete this Employer?');"
                        <asp:BoundField DataField="EmployerId" HeaderText="EmployerId" InsertVisible="False"
                            ReadOnly="True" SortExpression="EmployerId" />
                        <asp:BoundField ReadOnly=true DataField="BANumber" HeaderText="BANumber" SortExpression="BANumber" />
                        <asp:BoundField DataField="EmployerName" HeaderText="EmployerName" SortExpression="EmployerName" />
                        <asp:CheckBoxField DataField="ID4K" HeaderText="ID4K" SortExpression="ID4K" />
                        <asp:CheckBoxField DataField="ID4E" HeaderText="ID4E" SortExpression="ID4E" />
                        <asp:CheckBoxField DataField="ERSStatus" HeaderText="ERSStatus" SortExpression="ERSStatus" OnChange="javascript:return('Are you sure');"   />
                        <asp:CheckBoxField DataField="UploadStatus" HeaderText="UploadStatus" SortExpression="UploadStatus" />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ERSConnectionString1 %>"
                    SelectCommand="spGetEmployers" SelectCommandType="StoredProcedure"
                    UpdateCommandType="StoredProcedure" DeleteCommand="spDeleteEmployer" DeleteCommandType="StoredProcedure">
                        <asp:Parameter Name="BANumber" Type="Int32" />
                        <asp:Parameter Name="EmployerName" Type="String" />
                        <asp:Parameter Name="ID4K" Type="Boolean" />
                        <asp:Parameter Name="ID4E" Type="Boolean" />
                        <asp:Parameter Name="ERSStatus" Type="Boolean" />
                        <asp:Parameter Name="UploadStatus" Type="Boolean" />
                        <asp:Parameter Name="EmployerId" Type="Int32" />
                        <asp:Parameter Name="EmployerId" Type="Int32" />

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are two options :

                                <input id="ChkBxSelectedInfo" type="checkbox" style="width:30px;height:30px" onclick="return confirm('Are you sure you want to uncheck this ?');" />
                            <ItemStyle Width="5%" />



Protected Sub UserListGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles UserListGrid.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
   Dim chk As CheckBox = CType(e.Row.Cells(0).Controls(0), CheckBox)
   chk .Attributes.Add("onClick", 'return confirm('Are you sure you want to uncheck this ?');' );
End If
End Sub
sanagarwlAuthor Commented:

your second solution works beautifully, however I need to popup the message only when I'm going from checked to unchecked and I added the following modification but it does not seem to work --
I think I need to use some thing like chk.clientID.checked ...

thanks for the help


chk.Attributes.Add("onClick", "if (chk.checked==""false"") {return confirm('Please ensure there are no active records for this employer. Employer will not be able to modify/update/delete active records once change is made. If this is ok, please continue.');}")

Open in new window

Register the following script into the page.

function confirmDelete(obj) {
if  (obj.checked == false){
return confirm('Please ensure there are no active records for this employer. Employer will not be able to modify/update/delete active records once change is made. If this is ok, please continue.'); }
  return true;}

And modify the following line.

 chk .Attributes.Add("onClick", 'return confirmDelete(' + chk.ClientID  + ');' );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sanagarwlAuthor Commented:

thank you very much for your help, but I am unable to grab the state of the checkbox.

I have the following lines of code:
chk.Attributes.Add("onClick", "return confirmDelete(' + chk.ClientID + ');")

And as shown in the code below I am successfully registering the script into the page: If I comment out
scriptString += "if(true == true){ "
and uncomment
'scriptString += "if(obj.checked == false){ "
then nothing happens otherwise  the popup message *does* get displayed.

I have spent the better part of the morning on this and any help would be much appreciated...

 Dim scriptString As String = "<script language=""JavaScript""> function confirmDelete(obj) {"
        'scriptString += "if(obj.checked == false){ "
        scriptString += "if(true == true){ "
        scriptString += "return confirm('Please ensure there are no active records for this employer. Employer will not be able to "
        scriptString += "modify/update/delete active records once change is made. If this is ok, please continue.'); }"
        scriptString += "else{return true;}"
        scriptString += "}</"
        scriptString += "script>"
        Me.ClientScript.RegisterStartupScript(GetType(Page), "Startup", scriptString)

Open in new window

sanagarwlAuthor Commented:
the javascript function required document.getelementbyid(obj).checked..., thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.