我在页面上定义了一个用户控件,如下所示:
<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 == null。
我试图通过添加 DefaultValue 属性来指定默认值应该是“视图”,但它没有达到我想要的效果。
谁能发现我做错了什么?