Quick tip: Creating multiple user controls with simillar properties
Imagine you just created 4 or 5 or n different Web user controls in your ASP.NET WebForms application. Now you realized that they all share some properties. This is a problem, because it means you are having nearly identical code in multiple spots. What you can do is create a partial public class that inherits from System.Web.UI.UserControl class and put in all properties that your controls share. Then go to each and every of your user controls and change inheritance to your own class instead of System.Web.UI.UserControl.
Class from which your controls inherit:
public partial class MyControl : System.Web.UI.UserControl { #region Properties public string Title { get; set; } public string Singature { get; set; } #endregion }
This is how you use it in your controls (red represents original line):
public partial class MyControl1 : MyControl { protected void Page_Load(object sender, EventArgs e) { } ... } public partial class MyControl2 : MyControl { protected void Page_Load(object sender, EventArgs e) { } ... }
And now, you can do something like:
<mytag:mycontrol ID="mc1" runat="server" Title="My title" Signature="Lotushints" />
for every control that inherits MyControl class
Leave a Reply