如何设置 ASPX UserControl 属性的默认值?

发布于 2024-08-02 10:21:17 字数 881 浏览 8 评论 0原文

我在页面上定义了一个用户控件,如下所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随风而去 2024-08-09 10:21:17

视觉设计器和代码生成器使用 DefaultValueAttribute 来标识默认值,以便他们可以更智能地生成代码。在 Visual Studio 中,当属性返回的值与属性中声明的值不同时,此属性将导致该属性以粗体显示。

DefaultValueAttribute 实际上并没有为您设置属性的默认值。为此,只需在构造函数中指定合适的默认值即可。对于您的情况:

public partial class MyUserControl
{
    public MyUserControl()
    {
        MyCustomProperty = "View";
    }

    ...
}

另外,请注意您编码的属性将无法在回发中幸存。如果这是往返之间的重要行为,请务必将其添加到视图状态!

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:

public partial class MyUserControl
{
    public MyUserControl()
    {
        MyCustomProperty = "View";
    }

    ...
}

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!

萌无敌 2024-08-09 10:21:17

显式设置属性值而不是使用 DefaultValue 属性怎么样?

private string _MyCustomProperty = "View";
public string MyCustomProperty 
{
  get
  {
    return _MyCustomProperty;
  }
  set
  {
    _MyCustomProperty = value;
  }
}

How about setting the property value explicitly rather than using the DefaultValue attribute?

private string _MyCustomProperty = "View";
public string MyCustomProperty 
{
  get
  {
    return _MyCustomProperty;
  }
  set
  {
    _MyCustomProperty = value;
  }
}
从﹋此江山别 2024-08-09 10:21:17

如果您查看 MSDN 中关于 DefaultValue,你就会明白你做错了什么 -

DefaultValueAttribute 不会
使会员自动成为会员
使用属性初始化
价值。您必须设置初始值
在您的代码中。

If you take a look at the note given in MSDN about DefaultValue, you'll understand what you are doing wrong -

A DefaultValueAttribute will not
cause a member to be automatically
initialized with the attribute's
value. You must set the initial value
in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文