What's the best method for extending the functionality of a standard Web Server Control in ASP.NET?
Currently my code resembles this:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomPanel.ascx.cs"
CodeFile="CustomPanel.ascx.cs" CodeFileBaseClass="System.Web.UI.WebControls.Panel"
Inherits="MyProject.CustomPanel" %>
开发者_JAVA百科
...
namespace MyProject
{ public partial class CustomPanel : System.Web.UI.WebControls.Panel
{ ... }
}
However, I'm really not sure how to proceed from here, because this does not appear to follow a logical inheritance path that includes System.Web.UI.UserControl
Am I doing something wrong? How can I inherit Panel and extend standard event-handlers, such as PreInit?
Are you wanting to extend or encapsulate a Panel
here?
Extending it would be done not with a User Control, but with a normal class file, with the class inheriting from Panel
.
Encapsulating it would be done in a User Control, as appears to be what you have done here; In such a case, you put a Panel
instance inside the User Control's ascx file... you do not attempt to inherit from it.
It actually looks like you are trying to do both at the same time; you shouldn't do that. There is no direct inheritance path as you seem to be looking for that contains both Usercontrol and Panel; you'll have to choose one of the above options or the other.
To extend it, you should create a new class, which inherits the System.Web.UI.WebControls.Panel class. Once you have created your custom class, you can override existing methods from the Panel class, which are marked as virtual. (Typing the keyword override in Visual Studio, while inside your class, will pop up a list of virtual methods)
精彩评论