The below code runs a confirm messagebox with yes no on Asp.Net.
I need to detect the value if its confirmed or not.
How can I do this ?
Aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<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 save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
</form>
</body>
</html>
Code behind :
protected void OnConfirm(object sender, EventArgs e)
{
// This method runs even though the user clicks no.
}
Update :
With this code, both yes or no selections runs the same method named OnConfirm.
So I try to run the OnConfirm method only if the user clicks yes.
Update:
you can use a hidden field with runat=server and save yes/no. then send it to server.
<input type="hidden" runat="server" value="" id="hidden1" />
function Confirm() {
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
document.getElementById("hidden1").value = "yes";
} else {
confirm_value.value = "No";
document.getElementById("hidden1").value = "no";
}
document.forms[0].appendChild(confirm_value);
}
If you use a master page, remember that client id of hidden is different with its server id.
protected void OnConfirm(object sender, EventArgs e)
{
string confirmValue = hidden1.Value;
}
i think what you want to do is only execute the server side button when the user confirms OK right? if yes, then just do this, basically when the javascript function returns false, the server side button will not fire ie: (OnClientClick = "return Confirm()")
<script type = "text/javascript">
function Confirm() {
return confirm("Do you want to save data?");
}
</script>
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "return Confirm()"/>
Related
Confirm okay button is not working, its not calling server side method.
Buttons get disabled but server side function is not being called.
If i remove the following two lines from the confirm method then it works fine;
document.getElementById('btnSubmit').disabled = true;
document.getElementById('btnCancel').disabled = true;
Code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript">
function confirmPayment() {
var isOkay = confirm("Confirm Payment?");
if (isOkay) {
document.getElementById('btnSubmit').disabled = true;
document.getElementById('btnCancel').disabled = true;
return isOkay;
} else {
return isOkay;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" Width="110px" OnClientClick="return confirmPayment();"
Text="Submit Payment" ></asp:Button>
<asp:Button ID="btnCancel" runat="server" Width="110px"
Text="Cancel" CausesValidation="False"></asp:Button>
</div>
</form>
</body>
any thoughts ?
You don't have a server side method set on btnSubmit. There is no onClick only onClientClick
I would suggest you set the controls ClientIDMode="Static" just in case.
If you don't do this, then you should adopt this format:
var btn = document.getElementById('<%=Button1.ClientID%>');
Or in your case:
document.getElementById('<%=btnSubmit.ClientID%>).disabled = true;
Set timeout 0 has done the trick.
function confirmPayment() {
var isOkay = confirm("Confirm Payment?");
if (isOkay) {
setTimeout(function () {
document.getElementById('btnSubmit').innerText = 'Processing...';
document.getElementById('btnSubmit').disabled = true;
document.getElementById('btnCancel').disabled = true;
}, 0);
return isOkay;
} else {
//event.preventDefault();
return isOkay;
}
}
I want to display a message to the user to select yes or no. According to the clicked button(Yes or no), the value should be passed to a query as a parameter. How can I do this. This is what I have done so far.
<script type="text/javascript">
function confirm()
{
var confirmValue = document.createElement("INPUT");
confirmValue.type = "hidden";
confirmValue.name = "confirmValue";
if (confirm("Do you want to finalize? If you click yes, you will not be able to update and application will be passed to the MBD Coordinator")) {
confirmValue.value = "YES";
}
else
{
confirmValue.value = "NO";
}
document.forms[0].appendChild(confirmValue);
}
</script>
<asp:Button ID="btnFinish" runat="server" Text="Finish" BorderStyle="Solid" BorderColor="#9B0423"
Font-Bold="true" Width="100px" Height="30px" ForeColor="#9B0423" Style="margin-right:20px;" OnClick="btnFinish_Click" OnClientClick = "confirm()"/>
This is the C# code
string confirmValue = Request.Form["confirmValue"];
if (confirmValue == "Yes")
{
string value = "YES";
}
else
{
string value = "NO";
}
But in this I don't get a message to select any option.
Change the name of the function from "confirm" to something else. You are overriding the default confirm function in JavaScript.
I am using the following code to delete records from database using confirmation.
ASP.NET
<asp:TextBox ID="text_delete" Width="70%" Height="60px" ValidationGroup="del" CssClass="textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="required_desc" CssClass="validator" ValidationGroup="del" ControlToValidate="text_delete" ErrorMessage="Reason Required" runat="server">Reason Required</asp:RequiredFieldValidator>
<asp:Button ID="button_delete" CssClass="button" ValidationGroup="del" Text="Delete" runat="server" OnClientClick="Confirm()" OnClick="button_delete_Click" />
JavaScript
<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 delete this data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
C#
protected void button_delete_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
// Delete the data
}
if (confirmValue == "No")
{
// Do not delete data
}
}
But here i want the user to enter the reason to delete in textbox first and only after that the confirm popup should appear. Even though i am using a required field validator the validator is fired only after the popup window. How can i fire the validator first and only if the reason is entered the popup window for delete confirmation should appear. How can i do this?
You can invoke the required field validation within a script using Page_ClientValidate(), or Page_ClientValidate("group name") if you want to be more specific.
if(Page_ClientValidate()){
....popup confirmation code
....
}
Working RequiredFieldValidator along with javascript
I have ASP.NET WEB FORMS apps
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" />
function btnDeleteClick() {
var result = confirm("Want to delete all Deals?");
if (result) {
return true;
} else {
return false;
}
}
$("#MainContent_DeleteDealsButton").on("click", function() {
if (btnDeleteClick()) {})
On this button I have sample javascript alert to ask OK or Cancel.I want if choise OK to fire CODE BEHIND method call DeleteDealsButton_Click, if it Cancel to do nothing.
Just change button defination to,
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" OnClientClick="return confirm('Want to delete all Deals?')" />
No need for any javascript function.
try like this:
function btnDeleteClick() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
confirm_value.value = "No";
var result = confirm("Want to delete all Deals?");
if (result) {
confirm_value.value = "Yes";
__doPostBack('DeleteDealsButton', '')
return true;
} else {
confirm_value.value = "No";
return false;
}
}
In your .Aspx file Change your button code to like this:
<asp:Button ID="DeleteDealsButton" OnClientClick=" return btnDeleteClick()" runat="server" Text="Delete" OnClick="DeleteDealsButton_Click" />
In your .Aspx.cs file
protected void DeleteDealsButton_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (!String.IsNullOrWhiteSpace(confirmValue) && confirmValue.Equals("Yes"))
{
}
}
i want to show a javascript confirm box on the client side under OnChange event "chkIsActive_CheckedChanged". i am using registerStartupscript int the code behind for this purpose. this confirm message is displaying fine but it is displaying after end of the event "chkIsActive_CheckedChanged". i want to display the confirm message while executing in the function. Please help me out.
My HTML
<asp:CheckBox ID="lbl_IsActive" runat="server" OnCheckedChanged="chkIsActive_CheckedChanged" AutoPostBack="true" Checked='<%# Eval("IsActive") %>' ></asp:CheckBox>
My javascript
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.getElementById("<%=IsChecked.ClientID%>");
if (confirm("Do you want to save data?")) {
confirm_value.value = "1";
} else {
confirm_value.value = "0";
}
}
</script>
my C# code behind
protected void chkIsActive_CheckedChanged(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "Confirm();", true);
string confirmValue = IsChecked.Value;
if (confirmValue == "1")
{
string CarEstimateID = "";
Entities.CarEstimateFirms ObjEst = new Entities.CarEstimateFirms();
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
CarEstimateID = (GridView1.Rows[gr.RowIndex].FindControl("lbl_CarEstimateFirmID") as Label).Text; // GridView1.DataKeys[gr.RowIndex].Value.ToString();
ObjEst.CarEstimateFirmID = Convert.ToInt32(CarEstimateID);
ObjEst.IsActive = chk.Checked;
BLL.Common.UpdateCarEstimateFirms(ObjEst);
BindGridView();
}
}
Add onchange javascript event in your checkbox:
<asp:CheckBox ID="lbl_IsActive" runat="server" OnCheckedChanged="chkIsActive_CheckedChanged" AutoPostBack="true"
Checked='<%# Eval("IsActive") %>' onchange="javascript:return Confirm();" ></asp:CheckBox>
Your javascript confirm method should be like below:
<script type = "text/javascript">
function Confirm() {
if (confirm("Do you want to save data?")) {
return true;
} else {
return false;
}
}
</script>
And remove below line from chkIsActive_CheckedChanged event from code behind:
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "Confirm();", true);