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

私は1種類の解決策を見つけました:

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 に答える