ASP.NET Web 控件可以在构造函数中接受任何内容吗?
我想要一个安全的控件,但也想扩展 asp.net 框架提供的现有按钮或文本框。 所以我的类 dec 看起来像这样:
public class MyTextBox : System.Web.UI.WebControls.TextBox
我正在考虑执行此操作的策略模式之类的东西,但不确定如何将 ISecureControl 传递到 ctor 中,或者可能是 init 方法或其他东西?
我可能会回答我自己的问题。 我想我也许能够重写 OnInit 或 OnPreInit 或其他东西并创建并将其传递到那里,但不确定是否有一种方法可以在创建时以某种方式直接传递它?
override OnInit()
{
secureControl = new MySecureControl();
}
override OnRender()
{
if(secureControl.CanRender)
base.OnRender();
}
有没有更好的办法? 或者我在这里解决我自己的问题......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么 MyTextBox 不直接实现 ISecureControl?
why doesn't MyTextBox just implement ISecureControl?
是的,差不多就是这样了。 对于这种具体情况,我会继续前进。
如果 MySecureControl 有自己的构造函数参数,您可以研究依赖项注入,但这意味着您将使用类似 ObjectFactory.GetInstance<> 的内容,而不是使用 new 。 或者只是 ObjectFactory.BuildUp(this) 并使用属性注入。
别出汗:)
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 :)