asp.net服务器控件中的默认值
发布时间:2020-12-16 06:53:51 所属栏目:asp.Net 来源:网络整理
导读:我的默认值属性有问题. 当我在设计模式下将控件添加到页面时,默认值不起作用.这是我的代码: [DefaultProperty("Text")][ToolboxData("{0}:KHTLabel runat=server key=dfd/{0}:KHTLabel")]public class KHTLabel : Label,IKHTBaseControl{ [Bindable(true)]
我的默认值属性有问题.
当我在设计模式下将控件添加到页面时,默认值不起作用.这是我的代码: [DefaultProperty("Text")] [ToolboxData("<{0}:KHTLabel runat=server key=dfd></{0}:KHTLabel>")] public class KHTLabel : Label,IKHTBaseControl { [Bindable(true)] [Category("Appearance")] [DefaultValue("KHT")] [Localizable(true)] public string Key { get { String s = (String)ViewState["Key"]; return ((s == null) ? String.Empty : s); } set { ViewState["Key"] = value; } } protected override void RenderContents(HtmlTextWriter writer) {...... 但是,在设计模式下,当我从工具箱添加控件时,该键不存在 <cc1:KHTLabel ID="KHTLabel1" runat="server"></cc1:KHTLabel> 解决方法
这不是[DefaultValue]属性的作用,我担心.它的作用允许Visual Studio设计器(特别是“属性”网格)确定默认显示的内容,因此如何知道在默认值不同时将值显示为粗体.
您可以在代码中包含值“KHT”作为默认值. this 2008 blog posting of mine中有一些相关的细节. 下面的代码是相当简陋的,我无法验证它编译,但它应该让你知道如何处理“强制”DefaultValueAttributes的值到ViewState: private string GetDefaultAttributeValueForProperty(string propertyName) { var attributesForProperty = (from prop in typeof(KHTLabel).GetProperties() where prop.Name == propertyName select System.Attribute.GetCustomAttributes(prop)).First(); var defaultValueAttribute = (from attr in attributesForProperty where attr.GetType() == typeof(DefaultValueAttribute) select ((DefaultValueAttribute)attr).Value).FirstOrDefault(); return Convert.ToString(defaultValueAttribute); } public KHTLabel() { ViewState["Key"] = GetDefaultAttributeValueForProperty("Key"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 部署DotNetNuke并将ASP.NET应用程序分开 – 可能的问题?
- 请帮助我了解ASP.NET控件的AutoPostBack属性
- asp.net-web-api – 使用Asp.Net Core WebAPI进行授权
- asp.net-mvc-4 – DataAnnotation Range属性的客户端验证之
- asp.net – 如何使用master从asp:content页面访问母版页上
- .net – 列表框在autopostback上滚动到顶部
- asp.net-mvc-4 – 从beta版升级后的Autofac / MVC4 / WebAp
- ASP.net会话cookie丢失或删除
- asp.net – web.config中的多个/不同的身份验证设置
- ASP.NET获取当前用户名