[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

asp:checkboxfield javascript

Posted on 2008-11-03
5
Medium Priority
?
720 Views
Last Modified: 2013-11-07
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
Comment
Question by:sanagarwl
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:dhanushkad
ID: 22871400
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
 

Author Comment

by:sanagarwl
ID: 22872105
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
 
LVL 3

Accepted Solution

by:
dhanushkad earned 2000 total points
ID: 22872203
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
 

Author Comment

by:sanagarwl
ID: 22877436
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
 

Author Closing Comment

by:sanagarwl
ID: 31512879
the javascript function required document.getelementbyid(obj).checked..., thank you
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question