0

Extend an event in a user control with out changing in the associated class (usercontrol.cs) file, add as separate class in app_code ,is that possible? Reason: I am using a CMS (Ektron), so i cant add code in usercontrol.cs file

code sample:

user control ascx file

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Usercontrols_WebUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Click me" 
    onclick="Button1_Click" />

user control .cs file

 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("clicked the button");
    }

so i need to add some other code (eg: Response.Write("clicked needed button");) to 'Button1_Click' event by creating a new class or any other option to achieve this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Nikhil K S
  • 806
  • 1
  • 13
  • 27

2 Answers2

1

This is possible only if class in main .cs file is defined as partial class. Otherwise not.

  • as my research partial class method can't override in extended class with out creating new class inheriting the partial class. – Nikhil K S Oct 09 '13 at 14:20
0

Would handling the button click event on the aspx page suffice? If so then you'd need to expose the event from the control to the page. Have a look at the following post:

how to add an event to a UserControl in C#?

Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • sry that not i wanted, i want is add some logic to Button1_Click with out changing the usercontrol .cs file – Nikhil K S Oct 08 '13 at 06:08
  • 1
    the `Button1_Click` should have been written with a `virtual` keyword; you should derive the usercontrol class and override that method; and then use that derived instance in your aspx page. Else there is no other way...I think. //fingers crossed// – deostroll Oct 08 '13 at 06:17