Answered by:
How ask Confirmation message in asp C#

Question
-
User1827380138 posted
If User delete some data How to ask Yes/No (or) Ok/Cancel Questions in Asp.net C#
then Press Yes or Ok How to perform that task
if( Yes/Ok)
{
Delete.....
}
else if( No/Cancel)
{
Abort.....
}
How to Perform this Task in asp c#
Sunday, January 5, 2020 10:28 AM
Answers
-
User665608656 posted
Hi Dhoni,
There are many ways to delete data in C # (if it is in gridview).
In addition to directly adding the onclientclick event to the button mentioned by @PatriceSc, you can also add confirmation events to each delete button in the OnRowDataBound event of gridview.
I have a simple example of deleting data here, you can refer to it:
<asp:GridView DataKeyNames="EmployeeId" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="OnRowDeleting" OnRowDataBound = "OnRowDataBound"> <Columns> <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" /> <asp:BoundField DataField="FullName" HeaderText="FullName" /> <asp:CommandField ShowDeleteButton="True" ButtonType="Button" /> </Columns> </asp:GridView>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = BindGrid(); GridView1.DataBind(); } } private DataTable BindGrid() { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from Employees")) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string item = e.Row.Cells[0].Text; foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>()) { if (button.CommandName == "Delete") { button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"; } } } } protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e) { int index = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text); DeleteRecordByID(index); GridView1.DataSource = BindGrid(); GridView1.DataBind(); } private void DeleteRecordByID(int employeeId) { string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("DELETE FROM Employees WHERE EmployeeId = @EmployeeId", connection)) { command.Parameters.AddWithValue("@EmployeeId", employeeId); command.ExecuteNonQuery(); } } }
Here is the result of this work demo:
You can aslo refer to this link:
ASP.Net GridView - Delete Row with Confirmation
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 6, 2020 2:22 AM
All replies
-
User753101303 posted
Hi,
The easiest approach is to do that on the client side, right when clicking the delete button. For example you could start with something such as (returning false is to "cancel" the click event):
<asp:Button id="btnDelete" Text="Delete" runat="server" OnClientClick="if(!confirm('Delete?')) return false;"/>
and later enhance as needed. Keep in mind that your C# code runs on the server side to produce a response for an http request.The user can only act on the currently shown html page ie only between each request/response pair.
Sunday, January 5, 2020 1:19 PM -
User665608656 posted
Hi Dhoni,
There are many ways to delete data in C # (if it is in gridview).
In addition to directly adding the onclientclick event to the button mentioned by @PatriceSc, you can also add confirmation events to each delete button in the OnRowDataBound event of gridview.
I have a simple example of deleting data here, you can refer to it:
<asp:GridView DataKeyNames="EmployeeId" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="OnRowDeleting" OnRowDataBound = "OnRowDataBound"> <Columns> <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" /> <asp:BoundField DataField="FullName" HeaderText="FullName" /> <asp:CommandField ShowDeleteButton="True" ButtonType="Button" /> </Columns> </asp:GridView>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = BindGrid(); GridView1.DataBind(); } } private DataTable BindGrid() { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from Employees")) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string item = e.Row.Cells[0].Text; foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>()) { if (button.CommandName == "Delete") { button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"; } } } } protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e) { int index = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text); DeleteRecordByID(index); GridView1.DataSource = BindGrid(); GridView1.DataBind(); } private void DeleteRecordByID(int employeeId) { string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("DELETE FROM Employees WHERE EmployeeId = @EmployeeId", connection)) { command.Parameters.AddWithValue("@EmployeeId", employeeId); command.ExecuteNonQuery(); } } }
Here is the result of this work demo:
You can aslo refer to this link:
ASP.Net GridView - Delete Row with Confirmation
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 6, 2020 2:22 AM -
User-1038772411 posted
Hello Dhoni,
As we checked your requirement and solve the issue.
Attached below code for show delete and abort message based on client click.
.cshtml page
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <h3>Your application description page.</h3> <p>Use this area to provide additional information.</p> <script type="text/javascript"> function GetConfirmation() { var retVal = confirm("Do you want to delete ?"); if (retVal == true) { alert("Delete successfully"); } else { alert("Abort"); } } </script> <asp:Button id="btnConfirm" Text="Confirm" runat="server" OnClientClick="GetConfirmation()" /> </asp:Content>
OnClick event will work on codebehind page. OnClientClick will work on client side.
So OnClientClick event call first then after the OnClick event call.
If you still facing issue please let us know.
Thanks
Monday, January 6, 2020 7:17 AM -
User1827380138 posted
How to write Code in Coding Site
Monday, January 6, 2020 1:35 PM -
User475983607 posted
How to write Code in Coding Site
What is a "Coding Site"?
Monday, January 6, 2020 2:24 PM -
User-1038772411 posted
Hello Dhoni,
You can write Code in Coding Site as below.
About.aspx
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <h3>Your application description page.</h3> <p>Use this area to provide additional information.</p> <script type="text/javascript"> function GetConfirmation() { var retVal = confirm("Do you want to delete ?"); if (retVal == true) { var SendData = {}; $.ajax({ type: "POST", url: "About.aspx/DeleteStatus", data: JSON.stringify(SendData), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); }, error: function (result) { console.warn(result.statusText); } }); alert("Delete successfully"); } else { alert("Abort"); } } </script> <asp:Button id="btnConfirm" Text="Confirm" runat="server" OnClientClick="GetConfirmation()" /> </asp:Content>
About.aspx.cs Codebehind file
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string DeleteStatus(string parameter) { //Write logic that you want. }
If you still facing issue please let us know.
Thanks
Tuesday, January 7, 2020 6:38 AM