0

在不更改关联类(usercontrol.cs)文件的情况下扩展用户控件中的事件,在 app_code 中添加为单独的类,这可能吗?原因:我使用的是 CMS (Ektron),所以我无法在 usercontrol.cs 文件中添加代码

代码示例:

用户控制 ascx 文件

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

用户控制 .cs 文件

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

所以我需要通过创建一个新类或任何其他选项来添加一些其他代码(例如:Response.Write("clicked required button");) 到'Button1_Click'事件?

4

2 回答 2

1

只有当 main .cs 文件中的类被定义为部分类时,这才有可能。否则不行。

于 2013-10-09T13:05:51.653 回答
0

处理 aspx 页面上的按钮单击事件就足够了吗?如果是这样,那么您需要将事件从控件公开到页面。看看下面的帖子:

如何在 C# 中向 UserControl 添加事件?

于 2013-10-08T05:52:14.387 回答