ASP Web Forms fire code behind event on condition - 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"))
{
}
}

Related

My C# code does not run until I click again

im calling a JS function inside a c# method in ASP, this is my html:
<asp:Button runat="server" id="Button1" CssClass="btns" Text="test confirm delete" OnClick="testDelete"/>
and this is the testDelete method:
protected void testDelete(object sender, EventArgs e) {
int x = 0;
int y = 2;
int res = x + y;
if (res > 1) {
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "testDel()",true);
if (hc2.Value == "true") {
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Ok');", true);
//Here im gonna do a SQL Delete Query
}
}
}
and this is the JS function
function testDel() {
document.getElementById("hc2").value = confirm("you want to delete?").toString();
console.log(document.getElementById("hc2").value);
}
As you can see in the JS function i am assigning the confirm value to my "hc2" element of the html, and I use that same element in my C# code to check if the user clicked YES or CANCEL, and if the user clicked OK the Alert and the SQL query runs.
the assignment of the confirm value to "hc2" element works, but the alert does not work until I click the button again.
here's a bit of JavaScript you can use that replaces the code behind logic. Once you get a confirmation, call the codebehind function via JavaScript.
html:
<asp:button id="BtnConfirm" Runat="Server" OnClientClick="testDelete()" /> <br>
<asp:button id="BtnYes" Runat="Server" OnClick="ASPFunction" visible="false" />
JavaScript:
function testDelete() {
var x = 0;
var y = 2;
var res = x + y;
if (res > 1) {
var rslt = confirm("Do you want to delete?");
}
if (rslt == true) {
document.getElementById("BtnYes").click();
} else {
// block of code to be executed if the condition is false
}
}
and CodeBehind:
protected void BtnYes(object sender, EventArgs e) {
//sql stuff goes here
}

Confirm messagebox not working correctly

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()"/>

How to display "yes" or "no" message box in asp.net?

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.

Deleting data using confirmation window popup in ASP.NET and with RequiredFieldValidator

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

RegisterStartupScript shows message after function ends

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);

Categories