如何设置 ASPX UserControl 属性的默认值?
我在页面上定义了一个用户控件,如下所示:
<uc:MyUserControl ID="MyUserControl" runat="server" Visible="true" />
我想在不同页面上重用具有自定义属性的相同控件,如下所示:
<uc:MyUserControl ID="MyUserControl" runat="server" Visible="true"
MyCustomProperty="MyCustomText" />
MyCustomProperty 的目的是控制 MyUserControl 中的某些文本为我指定的任何文本。
对于第一种情况,我希望文本为“View”,对于第二种情况,我希望文本为“MyCustomText”。
在我的用户控件中,我有以下代码来定义属性:
[DefaultValue("View")]
public string MyCustomProperty { get; set; }
我还有以下代码来根据属性更新文本:
LinkButton buttonSelect = e.Item.FindControl("ButtonSelect") as LinkButton;
if(buttonSelect != null) buttonSelect.Text = MyCustomProperty;
实际发生的情况是,当在第一种情况下未提供自定义属性时,则 MyCustomProperty ==无效的。
我尝试通过添加 DefaultValue 属性来指定默认值应为“View”,但它没有达到我预期的效果。
谁能发现我做错了什么吗?
I have a user control defined on an page as follows:
<uc:MyUserControl ID="MyUserControl" runat="server" Visible="true" />
I am wanting to reuse the same control on a different page with a custom property as follows:
<uc:MyUserControl ID="MyUserControl" runat="server" Visible="true"
MyCustomProperty="MyCustomText" />
The purpose of MyCustomProperty is to control some text in MyUserControl to be whatever I specify it to be.
For the first case I want the text to be "View" and for the second case I want it to be "MyCustomText".
In my user control I have the following code to define the property:
[DefaultValue("View")]
public string MyCustomProperty { get; set; }
I also have the following code to update the text based on the property:
LinkButton buttonSelect = e.Item.FindControl("ButtonSelect") as LinkButton;
if(buttonSelect != null) buttonSelect.Text = MyCustomProperty;
What actually happens is that when the custom property isn't supplied in the first case then MyCustomProperty == null.
I've tried to specify that the default should be "View" by adding the DefaultValue attribute but it hasn't had the affect that I intended.
Can anyone spot what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视觉设计器和代码生成器使用
DefaultValueAttribute
来标识默认值,以便他们可以更智能地生成代码。在 Visual Studio 中,当属性返回的值与属性中声明的值不同时,此属性将导致该属性以粗体显示。DefaultValueAttribute
实际上并没有为您设置属性的默认值。为此,只需在构造函数中指定合适的默认值即可。对于您的情况:另外,请注意您编码的属性将无法在回发中幸存。如果这是往返之间的重要行为,请务必将其添加到视图状态!
The
DefaultValueAttribute
is used by visual designers and code generators to identify the default value, so they can more intelligently generate code. In Visual Studio, this attribute will cause a property to be shown in bold when the property returns a value that differs from the value declared in the attribute.DefaultValueAttribute
does not actually set the default value of the property for you. To do this, simply specify a suitable default value in your constructor. In your case:Also, be aware that the property as you have coded it will not survive Postbacks. If this is important behaviour between round trips, be sure to add it to view state!
显式设置属性值而不是使用 DefaultValue 属性怎么样?
How about setting the property value explicitly rather than using the DefaultValue attribute?
如果您查看 MSDN 中关于
DefaultValue
,你就会明白你做错了什么 -If you take a look at the note given in MSDN about
DefaultValue
, you'll understand what you are doing wrong -