在asp.net服务器控件的默认值 [英] default value in asp.net server control
问题描述
我有默认值属性的问题。
当我在设计模式下添加我的控制页面,默认值是行不通的。这是我的code:
[DefaultProperty(文本)
[ToolboxData(< {0}:KHTLabel RUNAT =服务器密钥= DFD>< / {0}:KHTLabel>中)
公共类KHTLabel:不干胶标签,IKHTBaseControl
{
[可绑定(真)
[类别(外观)]
[默认值(KHT)]
[本地化(真)
公共字符串键
{
得到
{
字符串s =(字符串)的ViewState [钥匙];
回报(?(S == NULL)的String.Empty:S);
} 组
{
的ViewState [钥匙] =值;
}
} 保护覆盖无效RenderContents(HtmlTextWriter的作家)
{......
不过,在设计模式,当我从工具箱中添加控件,该键不存在
< CC1:KHTLabel ID =KHTLabel1=服务器>< / CC1:KHTLabel>
这不是 [默认值]
属性做什么,我害怕。它所做的,它允许Visual Studio设计(特别是属性网格)来确定的什么默认显示的,因此的怎么知道显示值粗体当它不同于默认的
这是高达你在你的code保存值KHT作为默认值的东西。有一个在这2008博客中矿的。
以下code是很不成熟的,我一直无法核实汇编,但它应该给你一些想法,你可以如何处理强迫你的 DefaultValueAttribute的价值
s转换的的ViewState
:
私人字符串GetDefaultAttributeValueForProperty(字符串propertyName的)
{
变种attributesForProperty =(从typeof运算丙(KHTLabel).GetProperties()
其中,prop.Name == propertyName的
选择System.Attribute.GetCustomAttributes(丙))第一()。
VAR defaultValueAttribute =(从ATTR在attributesForProperty
其中,attr.GetType()== typeof运算(DefaultValueAttribute)
选择((DefaultValueAttribute)attr)使用.value的).FirstOrDefault(); 返回Convert.ToString(defaultValueAttribute);
}
公共KHTLabel()
{
的ViewState [钥匙] = GetDefaultAttributeValueForProperty(密钥);
}
I have a problem with the default value attribute.
When I add my control to page at design mode, default value does not work. This is my code:
[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)
{......
But, at design mode when I add a control from the toolbox, the key does not exist
<cc1:KHTLabel ID="KHTLabel1" runat="server"></cc1:KHTLabel>
That's not what the [DefaultValue]
attribute does, I'm afraid. What it does it allows the Visual Studio designer (specifically the "Properties" grid) to determine what to show by default and therefore how to know to show the value as bold when it's different from the default.
It's upto you to have something in your code that holds the value "KHT" as the default value. There's a bit of relevant detail in this 2008 blog posting of mine.
The following code is fairly rudimentary, and I haven't been able to verify it compiles, but it should give you some idea how you could handle "forcing" the value of your DefaultValueAttribute
s into the 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");
}
这篇关于在asp.net服务器控件的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!