在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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)