Hi
I have below control. I want where code behind of below line to be added. Is it to be defined in User Control Or Web Form from where modal popup has been called.
C#
Form Department
htmlTable.Append("");
htmlTable.Append("");C#
User Control
×
Cancel
DeleteThanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 27, 2024, 12:49 AM EDT
To determine where to add the code-behind for the
LinkButton, consider the following:User Control: If the
LinkButtonis part of a User Control, the event handlerlnkRecordDelete_Clickshould be defined within the code-behind file of that User Control. This keeps the logic encapsulated within the control, making it reusable across different pages.Web Form: If the
LinkButtonis directly on a Web Form (e.g., an ASPX page), the event handler should be defined in the code-behind file of that Web Form. This is appropriate if the control is specific to that page and not intended for reuse.Given your context, it seems the
LinkButtonis part of a modal defined within a User Control. Therefore, you should define thelnkRecordDelete_Clickevent handler in the code-behind file of the User Control.Here’s an example of how you might define the event handler in the User Control’s code-behind file:
protected void lnkRecordDelete_Click(object sender, EventArgs e) { // Your delete logic here }Thanks
Gowtham CpRank #1,552 · 1K reputationPosted Aug 27, 2024, 12:55 AM EDT
Hi Ramco,
You should define the
lnkRecordDelete_Clickmethod in the code-behind file of your User Control (MBox.ascx.cs). Here’s a clearer breakdown:1. User Control (.ascx) In your User Control markup, you have the
LinkButtonthat triggers the delete action:2. User Control Code-Behind (MBox.ascx.cs) You need to add the
lnkRecordDelete_Clickmethod in the code-behind file (MBox.ascx.cs) where you handle the delete logic. Here’s a sample implementation:protected void lnkRecordDelete_Click(object sender, EventArgs e) { // Your logic for deleting the record try { // Example: Your database deletion code here ShowMessage("Success", "Record deleted successfully.", "success"); } catch (Exception ex) { ShowMessage("Error", "Failed to delete record: " + ex.Message, "error"); } }3. Including the User Control in Your Web Form Make sure your User Control is correctly included in the Web Form from which it is being used. For example:
This setup ensures that the
lnkRecordDelete_Clickmethod is properly linked to theLinkButtonin your User Control.Let me know if you need any more details or further assistance!
Aman GuptaRank #193 · 12.4K reputationPosted Aug 27, 2024, 1:38 AM EDT
Hi Ramco,
The
lnkRecordDelete_Clickevent handler should be added in the code-behind file of the User Control if theLinkButtonis part of the User Control.Here’s how you can structure it:
User Control Code-Behind (e.g.,
YourUserControl.ascx.cs):protected void lnkRecordDelete_Click(object sender, EventArgs e) { // Your delete logic here }Web Form Code-Behind (e.g.,
YourWebForm.aspx.cs): If theLinkButtonis part of a User Control, the event handler is generally defined in the User Control’s code-behind. The Web Form’s code-behind would not need this handler unless you want to handle the delete action from the Web Form, in which case you would need to bubble the event from the User Control to the Web Form.If the
LinkButtonis directly in the Web Form, then place thelnkRecordDelete_Clickmethod in the Web Form’s code-behind.So, place the
lnkRecordDelete_Clickmethod in the code-behind of where theLinkButtonresides—most likely the User Control in your scenario.