在Asp.net用户控件设计中,有时需要对某个DropDownList,和当些控件的文字进行操作。例如,隐藏。
把文字设成LABEL可解决,但我们可以把文件包含到控件中。对DropDownList进行扩展,举一反三
可以扩展其它控件,可以也增加其它属性,如Css样式。具体代码如下:
把文字设成LABEL可解决,但我们可以把文件包含到控件中。对DropDownList进行扩展,举一反三
可以扩展其它控件,可以也增加其它属性,如Css样式。具体代码如下:
1 #region XDropDownList
2 /// <summary>
3 /// 辅助的XDropDownList
4 /// <remarks>便于组件中隐藏控件,文字与控件同时进行控制。
5 /// Author:Petter Liu http://wintersun.cnblogs.com
6 /// </remarks>
7 /// <example>
8 /// <![CDATA[
9 /// <%@ Register Assembly="Utility" Namespace="Utility.Controls" TagPrefix="diycontrol" %>
10 /// <diycontrol:XDropDownList ID="DdlProduct" runat="server" FrontText="产品一:" Width="120px" />
11 /// ]]>
12 /// </example>
13 /// </summary>
14 [AspNetHostingPermission(SecurityAction.Demand,
15 Level = AspNetHostingPermissionLevel.Minimal),
16 AspNetHostingPermission(SecurityAction.InheritanceDemand,
17 Level = AspNetHostingPermissionLevel.Minimal),
18 DefaultProperty("FrontText"),
19 ToolboxData("<{0}:XDropDownList runat=\"server\"> </{0}:XDropDownList>")]
20 public class XDropDownList : DropDownList
21 {
22 #region Properties
23 /// <summary>
24 /// Gets or sets the front text.
25 /// </summary>
26 /// <value>The front text.</value>
27 [Bindable(true),
28 Category("Appearance"),
29 DefaultValue(""),
30 Description("Before DropDownList Text"),
31 Localizable(true)]
32 public virtual string FrontText
33 {
34 get
35 {
36 string s = (string)ViewState["FrontText"];
37 return (s == null) ? String.Empty : s;
38 }
39 set
40 {
41 ViewState["FrontText"] = value;
42 }
43 }
44 #endregion
45
46 #region override void RenderBeginTag(HtmlTextWriter writer)
47 /// <summary>
48 /// Renders the HTML opening tag of the control to the specified writer. This method is used primarily by control developers.
49 /// </summary>
50 /// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output stream to render HTML content on the client.</param>
51 public override void RenderBeginTag(HtmlTextWriter writer)
52 {
53 if (!string.IsNullOrEmpty(FrontText))
54 writer.Write(FrontText);
55 base.RenderBeginTag(writer);
56 }
57 #endregion
58 }
59 #endregion
http://www.cnblogs.com/wintersun
2 /// <summary>
3 /// 辅助的XDropDownList
4 /// <remarks>便于组件中隐藏控件,文字与控件同时进行控制。
5 /// Author:Petter Liu http://wintersun.cnblogs.com
6 /// </remarks>
7 /// <example>
8 /// <![CDATA[
9 /// <%@ Register Assembly="Utility" Namespace="Utility.Controls" TagPrefix="diycontrol" %>
10 /// <diycontrol:XDropDownList ID="DdlProduct" runat="server" FrontText="产品一:" Width="120px" />
11 /// ]]>
12 /// </example>
13 /// </summary>
14 [AspNetHostingPermission(SecurityAction.Demand,
15 Level = AspNetHostingPermissionLevel.Minimal),
16 AspNetHostingPermission(SecurityAction.InheritanceDemand,
17 Level = AspNetHostingPermissionLevel.Minimal),
18 DefaultProperty("FrontText"),
19 ToolboxData("<{0}:XDropDownList runat=\"server\"> </{0}:XDropDownList>")]
20 public class XDropDownList : DropDownList
21 {
22 #region Properties
23 /// <summary>
24 /// Gets or sets the front text.
25 /// </summary>
26 /// <value>The front text.</value>
27 [Bindable(true),
28 Category("Appearance"),
29 DefaultValue(""),
30 Description("Before DropDownList Text"),
31 Localizable(true)]
32 public virtual string FrontText
33 {
34 get
35 {
36 string s = (string)ViewState["FrontText"];
37 return (s == null) ? String.Empty : s;
38 }
39 set
40 {
41 ViewState["FrontText"] = value;
42 }
43 }
44 #endregion
45
46 #region override void RenderBeginTag(HtmlTextWriter writer)
47 /// <summary>
48 /// Renders the HTML opening tag of the control to the specified writer. This method is used primarily by control developers.
49 /// </summary>
50 /// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output stream to render HTML content on the client.</param>
51 public override void RenderBeginTag(HtmlTextWriter writer)
52 {
53 if (!string.IsNullOrEmpty(FrontText))
54 writer.Write(FrontText);
55 base.RenderBeginTag(writer);
56 }
57 #endregion
58 }
59 #endregion