Reputation: 25
I am working on a Asp.Net application, currently i am trying to trigger a JavaScript Confirm()
popup from the code behind. I would like to popup without clicking any button_click
event.
IF not blnResult then
popup message with OK& CANCEL
IF OK THEN Exit Sub
ELSE
no display
END IF
I tried to do below things and it's not firing popup, please assist.
1) Created a button in ASPX
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm()"/>
JavaScript function
<script language="javascript" type = "text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
if (confirm("Do you want to delete all records?")) {
confirm_value1.value = "Yes";
} else {
confirm_value1.value = "No";
}
document.forms[0].appendChild(confirm_value1);
}
</script>
Code behind,
Public function GetConfirmation()
btnConfirm_Click(btnConfirm, EventArgs.Empty)
End Sub
Above line isn't firing the popup for me.
Upvotes: 0
Views: 2313
Reputation: 3216
If I have understood correctly then probably you want to call your code behind click event only when user confirms by click on yes. There are many of ways doing this. I have listed few of them. Choose which ever suits best for you.
Don't know why you have created a hidden input field. What is the purpose of creating it. In case you don't need hidden input filed you can try these out.
- Confirmation in HTML Tag
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="return confirm('Do you want to delete all records?');"/>
- Confirmation in JavaScript
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm();"/>
<script language="javascript" type="text/javascript">
function Confirm() {
return confirm("Do you want to delete all records?");
}
</script>
Or
<script language="javascript" type="text/javascript">
function Confirm() {
var result = confirm("Do you want to delete all records?");
return result;
}
</script>
If you do need to keep hidden input field then use below.
<script language="javascript" type="text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
var result = confirm("Do you want to delete all records?");
confirm_value1.value = result ? "Yes" : "No";
document.forms[0].appendChild(confirm_value1);
return result;
}
</script>
Upvotes: 1
Reputation: 1157
Just modify your javascript function.
<script language="javascript" type = "text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
if (confirm("Do you want to delete all records?")) {
confirm_value1.value = "Yes";
} else {
confirm_value1.value = "No";
}
document.forms[0].appendChild(confirm_value1);
return false;//this will prevent submit click.
} </script>
just add return statement at last line of function.
return false;
I hope this solution will help you.
Upvotes: 0