Hi
I have below modal Popup User Control.
In WebForm Department.aspx how to handle user has pressed Delete Button / Cancel Button
Code
×
Cancel
DeleteThanks

Željko PerićRank #644 · 3.7K reputationPosted Aug 25, 2024, 3:15 PM EDT
In your WebForm
Department.aspx, you can handle the button clicks for the "Delete" and "Cancel" actions as follows:1. Delete Button (
lnkRecordDelete) Handling:In the code-behind (
Department.aspx.cs), handle theOnClickevent for the delete button:protected void lnkRecordDelete_Click(object sender, EventArgs e) { // Logic for deleting the record // For example: int departmentId = GetSelectedDepartmentId(); // Retrieve the ID of the department to delete DeleteDepartmentById(departmentId); // Method to delete the department // Optionally, refresh the page or update the grid/list after deletion Response.Redirect("Department.aspx"); // Reloads the page after deletion }2. Cancel Button Handling:
Since the Cancel button is a standard HTML button (
), and it has thedata-dismiss="modal"attribute, it will automatically close the modal when clicked. If you want to do something additional when the Cancel button is clicked, such as clearing form fields or resetting states, you can use JavaScript:document.querySelector(".modal-footer button[data-dismiss='modal']").addEventListener("click", function () { // Additional logic for cancel action, e.g., resetting a form resetForm(); }); function resetForm() { // Example of resetting a form in the modal if needed document.getElementById("yourFormId").reset(); }The modal popup will close, and you can execute any necessary additional logic in the JavaScript function.
All the best, Željko Peric
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 27, 2024, 12:54 AM EDT
To handle the actions when the user presses the Delete or Cancel button in your modal popup on the
Department.aspxWebForm, you can follow these steps:Department.aspx.cs), create an event handler for thelnkRecordDelete_Clickevent. This is where you will write the logic to delete the record.protected void lnkRecordDelete_Click(object sender, EventArgs e) { // Your logic to delete the record // For example: int recordId = Convert.ToInt32(hiddenFieldRecordId.Value); DeleteRecord(recordId); // Optionally, you can close the modal and refresh the page or update the UI ScriptManager.RegisterStartupScript(this, this.GetType(), "closeModal", "$('#myModal').modal('hide');", true); Response.Redirect(Request.RawUrl); // Refresh the page } private void DeleteRecord(int recordId) { // Your logic to delete the record from the database }Handle the Cancel Button Click Event:
data-dismiss="modal"attribute, which will close the modal when clicked. No additional server-side code is needed for this action.Optional: Use a Hidden Field to Pass Data:
$('#myModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget); // Button that triggered the modal var recordId = button.data('record-id'); // Extract info from data-* attributes $('#').val(recordId); });By following these steps, you can effectively handle the Delete and Cancel actions in your modal popup.
Thanks