[RESOLVED]To display an alert box to confirm according to the Check box True/False condition.

Hi Friends,

I have a gridview and the data entered into the gridview is through textboxes at the top.After entering all the data,before clicking the insert button,a check box is to be checked if the entry is confirmed and if the checkbox is not checked,the entry is
provisional. When clicking on the INSERT button, i need a confirm alert box to appear asking whether " Do u need to continue? Its a confirmed Entry !" if the checkbox is checked. if not checked, Alert box should appear as " Do u need to continue? Its a provisional
Entry !". If the user clicks cancel,the control should remain there. If the user clicks OK, the insert operation should take place. 

Below is the code for Insert button and checkbox. The form name is Presences.aspx.

<asp:CheckBox ID="chkCon" runat="server" onfocus="Checkbox()" AutoPostBack="True" />

<asp:Button ID="btnInsert" runat="server" Text="INSERT"/>

Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click

‘Code to diplay alert box.

Dim CSM As ClientScriptManager = Page.ClientScript
If chkCon.Checked = False Then
Dim strconfirm As String = "<script>if(!window.confirm(‘Are you sure this is a provional presences?’)){window.location.href=’Presences.aspx’;}</script>"
CSM.RegisterClientScriptBlock(Me.[GetType](), "Confirm", strconfirm, False)
ElseIf chkCon.Checked = True Then
Dim strconfirm As String = "<script>if(!window.confirm(‘Are you sure this is a confirmed presences?’)){window.location.href=’Presences.aspx’;}</script>"
CSM.RegisterClientScriptBlock(Me.[GetType](), "Confirm", strconfirm, False)
End If

cmd.CommandText = "Insert into TBL_PRESENCES values (" & txtEmpID.Text & ",’" & txtEmpName.Text.ToString & "’," & Session("LocID1") & ",’" & ddlLocName.Text.ToString & "’," & Session("JobCode1") & ",@FromDate,@ToDate," & txtNoDays.Text & ",’" & ddlStatus.Text.ToString
& "’," & Session("LocID2") & ",’" & ddlChargedTo.SelectedItem.Text.ToString & "’,’" & Session("JobCode2") & "’,’" & Session("BU").ToString & "’,@CreatedDate,’" & Session("PRname") & "’,’" & ddlManager.Text.ToString & "’," & Actual.ToString & "," & Provisional.ToString
& ")"
cmd.Parameters.Add(New OleDbParameter("@FromDate", txtFromDate.Text.ToString))
cmd.Parameters.Add(New OleDbParameter("@ToDate", txtToDate.Text.ToString))
cmd.Parameters.Add(New OleDbParameter("@CreatedDate", Date.Now.Date))
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
lblMsg.Text = "Record Inserted"
bindPresences()
GvPresences1.Visible = True

End sub.

In my code to display alert box, if i click OK or Cancel, the values get inserted to the Grid.Some one please help me with a client side script to solve this issue.

Thanks.

Hi sinjumujeeb,

Thanks for your post.

sinjumujeeb

When clicking on the INSERT button, i need a confirm alert box to appear asking whether " Do u need to continue? Its a confirmed Entry !" if the checkbox is checked. if not checked, Alert box should appear as " Do u need to continue? Its a provisional Entry
!". If the user clicks cancel,the control should remain there. If the user clicks OK, the insert operation should take place. 

According to your description, you could try the below code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function () {
            $("#btnInsert").click(function () {
                var ischecked = $("#chkCon").is(":checked");
                //alert(ischecked);
                if (ischecked) {
                    return confirm('Do u need to continue? Its a confirmed Entry !');
                }
                else {
                    return confirm('Do u need to continue? Its a provisional Entry  !');
                }
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:CheckBox ID="chkCon" runat="server" onfocus="Checkbox()" />
            <asp:Button ID="btnInsert" runat="server" Text="INSERT" />
        </div>
    </form>
</body>
</html> 
Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        'insert data into your database
End Sub

If you have any question about my reply, please let me know freely.

Best Regards,

Fei Han

Fei Han – MSFT

<script src="../../Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function () {
            $("#btnInsert").click(function () {
                var ischecked = $("#chkCon").is(":checked");
                //alert(ischecked);
                if (ischecked) {
                    return confirm('Do u need to continue? Its a confirmed Entry !');
                }
                else {
                    return confirm('Do u need to continue? Its a provisional Entry  !');
                }
            })
        })
    </script>

Dear Friend,

Thank you so much for your reply. Can u please send me the Jquery source code. I downloaded a code but when i tested its not working and alert box does’nt appear.

I also tried by converting your jquery code into javascript like below.

<script language="javascript" type="text/javascript">
function InsertCheck() {
var ischecked = document.getElementById(‘<%=chkCon.ClientID%>’).checked;
alert(ischecked);
if (ischecked) {

return confirm(‘Do u need to continue? Its a confirmed Entry !’);
}
else {
return confirm(‘Do u need to continue? Its a provisional Entry !’);
}

}

</script>

<asp:Button ID="btnInsert" runat="server" Text="INSERT" onclientclick="InsertCheck()" />

Here confirm alert box is appearing.If i click ok or cancel, Insert Operation is working. When i click cancel, i need to stay in the same page without insertion to happen. What should i do for that? please help.

sinjumujeeb

Can u please send me the Jquery source code. I downloaded a code but when i tested its not working and alert box does’nt appear.

Just add this

<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>

sinjumujeeb

<asp:Button ID="btnInsert" runat="server" Text="INSERT" onclientclick="InsertCheck()" />

You should return the value OnClientClick. Like below

<asp:Button ID="btnInsert" runat="server" Text="INSERT" onclientclick="return InsertCheck()" />

Thanks Friend. Now everything is working fine. Below is the javascript code and corrected textbox HTML.

<script language="javascript" type="text/javascript">
function InsertCheck() {
var ischecked = document.getElementById(‘<%=chkCon.ClientID%>’).checked;
if (ischecked) {
return confirm(‘Do u need to continue? Its a confirmed Entry !’);
}
else {
return confirm(‘Do u need to continue? Its a provisional Entry !’);
}
}
</script>

<asp:Button ID="btnInsert" runat="server" Text="INSERT" onclientclick="return InsertCheck()"/>

Leave a Reply