views:

303

answers:

2

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...

+1  A: 

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 :)

eglasius
+2  A: 

why doesn't MyTextBox just implement ISecureControl?

public class MyTextBox : TextBox, ISecureControl
ob
That wouldn't help me, then I'd have to implement a CanRender per control. I want to just have a SecureControl tell my TextBox if it can render or not.
rball