Question

I want to pass list of values to web user control from page.

Something like this:

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

How to create some kind of key-value pair property (dictionary, hashtable) in web user control.

Was it helpful?

Solution 2

I have found one kind of solution:

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

And on the page:

<%@ 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>

OTHER TIPS

You could create a public Dictionary property in the code behind of your user control:

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

Then in the code-behind of the form where you create the new user control, you can populate that new property:

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

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

MyUserControl.NameValuePair = newDictionary;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top