• 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?

thanks
<td>
                <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" />
                    
                    <Columns>
                        <asp:CommandField ButtonType="Image"
                                CancelImageUrl="~/Includes/Images/delete.gif" 
                                EditImageUrl="~/Includes/Images/edit.GIF"
                                ShowEditButton="True"
                                showheader=true
                                UpdateText="Update"
                                HeaderText="Action"
                                DeleteImageUrl="~/Includes/Images/delete.gif"
                                UpdateImageUrl="~/Includes/Images/statusicon_completed.gif" />
                        <asp:TemplateField HeaderText="Delete">
                            <itemtemplate>
                                <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:ImageButton>
                            </itemtemplate>
                        </asp:TemplateField>
                        <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" />
                    </Columns>
                </EnhancedControls:CustomGridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ERSConnectionString1 %>"
                    SelectCommand="spGetEmployers" SelectCommandType="StoredProcedure"
                    UpdateCommand="spModifyEmployer"
                    UpdateCommandType="StoredProcedure" DeleteCommand="spDeleteEmployer" DeleteCommandType="StoredProcedure">
                    <UpdateParameters>
                        <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" />
                    </UpdateParameters>
                    <DeleteParameters>
                        <asp:Parameter Name="EmployerId" Type="Int32" />
                    </DeleteParameters>
                </asp:SqlDataSource>
            </td>

Open in new window

0
sanagarwl
Asked:
sanagarwl
  • 3
  • 2
1 Solution
 
dhanushkadCommented:
There are two options :

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

or

2.

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
 
0
 
sanagarwlAuthor Commented:
dhanushkad:

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

0
 
dhanushkadCommented:
Register the following script into the page.

<script>
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.'); }
else{
  return true;}
}
</script>

And modify the following line.

 chk .Attributes.Add("onClick", 'return confirmDelete(' + chk.ClientID  + ');' );
0
 
sanagarwlAuthor Commented:
dhanushkad:

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

0
 
sanagarwlAuthor Commented:
the javascript function required document.getelementbyid(obj).checked..., thank you
0
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