【发布时间】:2013-04-24 07:14:26
【问题描述】:
我找到了一种使用键值的解决方案,但问题是当我在 .Cs 上使用它时 MyUserControl1.Param.Key = "区域"; MyUserControl1.Param.Value = 面积
它不允许我这样做...下面是代码...
public partial class MyUserControl : System.Web.UI.UserControl
{
private Dictionary<string, string> labels = new Dictionary<string, string>();
public LabelParam Param
{
private get { return null; }
set
{
labels.Add(value.Key, value.Value);
}
}
public class LabelParam : WebControl
{
public string Key { get; set; }
public string Value { get; set; }
public LabelParam() { }
public LabelParam(string key, string value) { Key = key; Value = value; }
}
}
If I use it aspx page like below it work fine:
<%@ Register src="MyUserControl.ascx" tagname="MyUserControl" tagprefix="test" %>
<test:MyUserControl ID="MyUserControl1" runat="server">
<Param Key="d1" value="ddd1" />
<Param Key="d2" value="ddd2" />
<Param Key="d3" value="ddd3" />
</test:MyUserControl>
【问题讨论】:
-
发布尝试使用该属性的代码?阅读:stackoverflow.com/questions/2257829/…
-
只有公共 setter 和私有 getter 实现且始终返回
null的属性是代码异味。只需创建一个SetParam方法即可。 -
我也公开了 getter,但仍然无法将值设置为 'param'
标签: c# asp.net user-controls