我發現了一種使用鍵值的解決方案,但問題是,當我在.Cs上使用它時,如: MyUserControl1.Param.Key =「Area」; MyUserControl1.Param.Value =面積如何在.CS端設置Key-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>
張貼嘗試使用屬性的代碼?閱讀此:http://stackoverflow.com/questions/2257829/access-child-user-controls-property-in-parent-user-control – Fabske 2013-04-30 11:26:25
一個屬性只有一個公共setter和一個私人的getter實現總是返回null '是一種代碼味道。只需創建一個'SetParam'方法。 – 2013-04-30 11:40:09
我讓getter也是公開的,但是仍然無法將值設置爲'param' – Sneha 2013-04-30 12:02:37