0

我想将值列表从页面传递给 Web 用户控件。

像这样的东西:

<uc:MyUserControl runat="server" id="MyUserControl">
    <DicProperty>
        <key="1" value="one">
        <key="2" value="two">
               ...
    </DicProperty>  
</uc:MyUserControl>

如何在 Web 用户控件中创建某种键值对属性(字典、哈希表)。

4

2 回答 2

0

Dictionary您可以在用户控件后面的代码中创建一个公共属性:

public Dictionary<int, string> NameValuePair { get; set; }

然后在创建新用户控件的表单的代码隐藏中,您可以填充该新属性:

Dictionary<int, string> newDictionary = new Dictionary<int, string>();

newDictionary.Add(1, "one");
newDictionary.Add(2, "two");
newDictionary.Add(3, "three");

MyUserControl.NameValuePair = newDictionary;
于 2013-03-26T04:56:14.633 回答
0

我找到了一种解决方案:

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; }
    }
}

在页面上:

<%@ 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>
于 2013-03-26T14:36:01.083 回答