asp.netasp.net-webcontrolasp.net-customcontrol

Can ASP.NET web controls take in anything in the constructor?


I want to have a secure control but also want to extend the existing button or textbox the asp.net framework provides. So my class dec would look something like:

public class MyTextBox : System.Web.UI.WebControls.TextBox

I was thinking something like a strategy pattern for doing it, but not sure how I could pass in a ISecureControl into the ctor or maybe an init method or something?

I might be answering my own question. I guess I might be able to override a OnInit or OnPreInit or something and create and pass it there, but wasn't sure if there's a way to just directly pass it in somehow at creation?

override OnInit()
{
  secureControl = new MySecureControl();
}

override OnRender()
{
  if(secureControl.CanRender)
    base.OnRender();
}

Is there a better way? or am I solving my own problem here...


Solution

  • yes, that's pretty much it. For that specific situation, I would move on. If MySecureControl had its own constructor parameters, you could look into dependency injection, but that would mean instead of using new you would use something like ObjectFactory.GetInstance<> or just ObjectFactory.BuildUp(this) and use property injection. Don't sweat it :)