我找到了一种使用键值的解决方案,但问题是当我在.C上使用它时 MyUserControl1.Param.Key =“Area”; MyUserControl1.Param.Value = Area
它不允许我这样做......下面是代码...
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>
答案 0 :(得分:0)
当你使用你提到的Param属性时:
MyUserControl1.Param.Key = "Area"
MyUserControl1.Param.Value = Area
您将收到错误,因为您正在访问Param属性的get部分。该属性的get部分的实现总是返回null,这将导致您的代码失败并可能出现NullRefrenceException。
除此之外,你的控件在你的字典中包含几个keyvaluepair,因此使用Param属性访问值是没有意义的。
尝试添加以下属性:
public IDictionary<string,string> Labels
{
get
{
return labels;
}
}
然后您可以访问以下值:
myControl.Labels["Key"] = value;