• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 738
  • Last Modified:

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

  • 3
  • 2
1 Solution
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  + ');' );
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now