1. This is of course possible.
Suppose, following is the
BaseUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BaseUserControl.ascx.cs" Inherits="BaseUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
You need to register this user control in another user control (
ChildUserControl
) and use that as follows:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChildUserControl.ascx.cs" Inherits="ChildUserControl" %>
<%@ Register src="BaseUserControl.ascx" tagname="BaseUserControl" tagprefix="uc1" %>
<uc1:BaseUserControl ID="BaseUserControl1" runat="server" /
>
And, if you register the
ChildUserControl.ascx
in an aspx page, you will see the TextBox from the
BaseUserControl.ascx
will be shown
2. Yes, it might be possible to inherit the code behind of a user control by anothe user control, but, it is suggested to put the base user control codes inside a base class (That inherits the
System.Web.UI.UserControl
) and then inherit the class by the code behind classes of the user controls. That is:
class UserControlBase : System.Web.UI.UserControl
{
}
class ChildUserControl : UserControlBase
{
}