将 Web 控件引用传递给用户控件基类
我创建了几个用户控件 - 大多数包含一个 Web 控件(文本框、下拉列表、单选按钮等) - 以及一个或多个验证控件。重点是将控制和验证结合在单个用户控件中。
我为这些用户控件创建了一个具有一些常见功能的基类 - 单个 Web 控件的多个属性的设置器,特别是要在 ascx 的控件中设置的 CssClass 和 Style。
例如,带有单个必填字段验证器的单个文本框。
基类的示例代码:
public WebControl ctrl {get; set;} //allow derived class access to this
public string CssClass
{
set { ctrl.CssClass = value; } //allow CssClass to be set in the aspx page
}
派生类的示例代码: (在构造函数或控件 OnInit 事件中 - 或?)
base.ctrl = txt; //tell the base class which web control to apply common properties to.
public string ErrorMessage
{
set { val.ErrorMessage = value;} //this works !
}
ascx 的示例代码:
<asp:TextBox ID="txt" Cssclass="input-text-m" maxlength="50" runat="server" />
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txt"
ErrorMessage="">*</asp:RequiredFieldValidator>
aspx 的示例代码:
<uc:TextBox ID="ForeName" Cssclass="input-text-m" maxlength="50"
ErrorMessage="Forename" runat="server"/>
我发现的问题是我找不到派生类在基类之前设置基类 Web 控件引用的方法调用属性设置器。 如果我在派生类构造函数中设置 base.ctrl - 那么此时派生类控件引用 (txt) 仍然为 null。 如果我在任何控制事件中设置base.ctrl - 例如OnInit - 那么就太晚了。
到目前为止,我已经通过简单地不使用基类,而是在用户控件类中编写属性设置器代码来解决这个问题,但这意味着代码重复,这是我试图避免的。
有没有办法通知控件的基类,我希望它在设置属性之前设置它们 - 或者我是否以错误的方式处理事情......
I created several user controls - most containing a single web control (text box, drop down, radio button etc) - along with one or more validation controls. The point being to combine control and validation in a single user control.
I created a base class for these user control with some common functionality - setters for several properties of a single web control, specifically CssClass and Style to be set in the control in the ascx.
Eg a single text box with a single required field validator.
Sample code for the base class:
public WebControl ctrl {get; set;} //allow derived class access to this
public string CssClass
{
set { ctrl.CssClass = value; } //allow CssClass to be set in the aspx page
}
Sample code for derived class:
(in constructor or control OnInit Event - or ?)
base.ctrl = txt; //tell the base class which web control to apply common properties to.
public string ErrorMessage
{
set { val.ErrorMessage = value;} //this works !
}
Sample code for ascx:
<asp:TextBox ID="txt" Cssclass="input-text-m" maxlength="50" runat="server" />
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txt"
ErrorMessage="">*</asp:RequiredFieldValidator>
Sample code for aspx:
<uc:TextBox ID="ForeName" Cssclass="input-text-m" maxlength="50"
ErrorMessage="Forename" runat="server"/>
The problem I found was that I couldn't find a way for the derived class to set the base class web control reference before the base classes property setters are called.
If I set base.ctrl in the derived class constructor - then the derived class control reference (txt) is still null at this point.
If I set base.ctrl in any of the control events - eg OnInit - then this is too late.
So far I have got around the problem by simply not using a base class, and writing the property setter code in the user control class instead, however this means duplication of code, which I was trying to avoid.
Is there a way to inform the base class of the control I want it to set the properties for in advance of them being set - or am I going about things the wrong way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在任何 get/set 操作之前调用
EnsureChildControls
并在EnsureChildControls
中包含 ctrl = txt 的 set 操作怎么样?对于普通服务器控件来说,这是相当标准的做法,我认为它也适用于用户控件。覆盖
EnsureChildControls
,保留对base的调用,并在对base的调用之后在此处设置ctrl = txt;
。详细信息:http://msdn.microsoft。 com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx
What about calling
EnsureChildControls
before any get/set operations and including the set operation for ctrl = txt inEnsureChildControls
? This is pretty standard practice for a normal servercontrol, I would think it would work for UserControls too.Override
EnsureChildControls
, leaving in the call to base, and setctrl = txt;
here after the call to base.More information: http://msdn.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx