Hi
I have below modal in User Control. How in the calling program it will be handled whether user has pressed Delete Button / Cancel Button
Code
×
Cancel
DeleteThanks

Hanif HefazRank #883 · 2.4K reputationPosted Aug 25, 2024, 1:58 AM EDT
Hello Ramco,
To handle the suituation for the delete or cancel button, In the code-behind of the calling program, you can use the Page_Load or Page_Init event to register event handlers for the modal's buttons. For the "Delete" button, you can use the lnkRecordDelete_Click event handler that you have already defined in your user control, and for the "Cancel" button, you can use the data-dismiss="modal" attribute, which will automatically close the modal when clicked.
protected void Page_Load(object sender, EventArgs e) { // Here register event handler for the "Delete" button lnkRecordDelete.Click += LnkRecordDelete_Click; } private void LnkRecordDelete_Click(object sender, EventArgs e) { // Here check the sender object to find which button was clicked if (sender is LinkButton) { // "Delete" button was clicked // Perform the delete operation or any other necessary logic here // ... // ... } else { // "Cancel" button was clicked (or any other button if you have) // You don not need to handle this case explicitly, because the modal closes automatically :) } }Try this and reach me in case of any problem.
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 25, 2024, 3:34 AM EDT
Hi Hanif
Modal is a user control. Suppose i am calling this modal form from Department Webform. How i will handle Delete/Cancel click option
Thanks
Hanif HefazRank #883 · 2.4K reputationPosted Aug 25, 2024, 4:19 AM EDT
Expose the click events in user control and subscribe to these events in the parent web form.
Let me add the coding hints...
public partial class MyModalUserControl : System.Web.UI.UserControl { public event EventHandler DeleteButtonClicked; public event EventHandler CancelButtonClicked; protected void lnkRecordDelete_Click(object sender, EventArgs e) { DeleteButtonClicked?.Invoke(this, EventArgs.Empty); } protected void Page_Load(object sender, EventArgs e) { lnkRecordDelete.Click += lnkRecordDelete_Click; } }And now subscribe in the web form:
public partial class DepartmentWebForm : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyModalUserControl modal = (MyModalUserControl)FindControl("myModalUserControl"); // Subscription... modal.DeleteButtonClicked += Modal_DeleteButtonClicked; modal.CancelButtonClicked += Modal_CancelButtonClicked; } private void Modal_DeleteButtonClicked(object sender, EventArgs e) { // Your delete logic... } private void Modal_CancelButtonClicked(object sender, EventArgs e) { // Cancel button clicked. } }