ASP.NET 服务器控件中的默认值
我对默认值属性有疑问。
当我在设计模式下将控件添加到页面时,默认值不起作用。这是我的代码:
[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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕这不是
[DefaultValue]
属性的作用。它的作用是让 Visual Studio 设计器(特别是“属性”网格)确定默认显示的内容,以及如何知道将值显示为粗体 strong> 当它与默认值不同时。您可以在代码中保留“KHT”值作为默认值,这取决于您。 此中有一些相关细节我的 2008 年博客文章。
以下代码相当初级,我无法验证它是否可以编译,但它应该可以让您了解如何将
DefaultValueAttribute
的值“强制”到视图状态
: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 theViewState
:DefaultValueAttribute 不用于设置属性的值。序列化器使用它来确定是否应该序列化该值。您需要在构造函数(或 OnInit)中设置属性的默认值。如果属性值与 DefaultValueAttribute 值匹配,则使用 DefaultValueAttribute 会使序列化数据更小。
The DefaultValueAttribute is not used to set the value for the property. It is used by the serializer to determine wheather it should serialize the value or not. You need to set your default value for the property in the constructor (or in OnInit). Using the DefaultValueAttribute makes the serialized data smaller if the property-value matches the DefaultValueAttribute value.
您可以获得您在第一个答案的评论中提到的结果 (
) 通过在ToolboxDataAttribute
中显式命名它。为了使其成为实际的默认值,您仍然必须在属性的 getter 中返回该值。这会导致在您的类中将相同的值重复 3 次。
顺便说一句,我不明白为什么您当前的
ToolboxData
中有key=dfd
,而属性名称是Key
并且是字符串类型。You can get the result you mentioned in your remark on this first answer (
<cc1:KHTLabel ID="KHTLabel1" runat="server" Key="KHT"></cc1:KHTLabel>
) by explicitly naming it in theToolboxDataAttribute
.To make this also be the actual default value, you still have to return that value in the getter of the property. It results in duplicating the same value three times in your class.
BTW, I don't understand why you currently have
key=dfd
in yourToolboxData
, while the property name isKey
and is of type string.