我在一个页面上定义了一个用户控件,如下所示:
<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; }
我还有以下代码来基于属性更新文本:
```html
我还有以下代码来基于属性更新文本:
```LinkButton buttonSelect = e.Item.FindControl("ButtonSelect") as LinkButton;
if(buttonSelect != null) buttonSelect.Text = MyCustomProperty;
实际上,在第一个情况下,如果没有提供自定义属性,则MyCustomProperty == null。
我尝试使用DefaultValue属性指定默认值为“View”,但它并没有产生我期望的影响。
有人能发现我做错了什么吗?