Reputation: 13
I have a script for a confirm pop up window.When I call this in our aspx.cs page it returns different values.My script is
<script type="text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to remove this employee from this group?")) {
confirm_value.value = "1";
} else {
confirm_value.value = "2";
}
document.forms[0].appendChild(confirm_value);
}
Button
<asp:Button ID="btnAdd" OnClientClick = "Confirm()" runat="server" ValidationGroup="1" onclick="btnAdd_Click" />
Button click function
protected void btnAdd_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "1")
{
//Do something
}
else
{
//Do something
}
}
First time I clicked cancel then my confirmvalue=="1"
and next time again I selected ok then my confirmvalue=="1,2"
in place of 2. How it returns error value.
Upvotes: 0
Views: 1587
Reputation: 58
function Confirm() {
var Result=confirm("Do you want to remove this employee from this group?");
var confirm_value = document.querySelector('[name="confirm_value"]');
if (Result) {
return true;
} else {
return false;
}
}
Upvotes: 1
Reputation: 27823
You're creating multiple inputs named "confirm_value", one each time you're clicking the button. What you need to do is reuse the same input:
function Confirm() {
var confirm_value = document.querySelector('[name="confirm_value"]');
if (!confirm_value) {
confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
document.forms[0].appendChild(confirm_value);
}
if (confirm("Do you want to remove this employee from this group?")) {
confirm_value.value = "1";
} else {
confirm_value.value = "2";
}
}
Upvotes: 4