0

我试图弄清楚如何创建一个基本上是 ExpandoObject 的 Web 服务器控件。

希望在 aspx 标记中创建控件时自动在控件上创建一个属性。

例如:

<x:ExpandoControl someProperty="a value"></x:ExpandoControl>

其中 someProperty 属性尚不作为控件上的属性存在。

我还应该提到,我并不严格需要 Control 或 WebControl 的任何功能。我只需要能够使用 runat="server" 在标记中声明它(这本身可能要求它是一个控件,至少我是这么想的)。

可能吗?如果是这样,我该如何开始?

非常感谢。

4

1 回答 1

1

我认为您的第一个赌注是实施IAttributeAccessor

public interface IAttributeAccessor
{
    string GetAttribute(string key);
    void SetAttribute(string key, string value);
}

ASP.NET 页面分析器为它不能映射到公共属性的每个属性调用IAttributeAccessor.SetAttribute 。

所以也许你可以从

public class ExpandoControl : Control, IAttributeAccessor
{
    IDictionary<string, object> _expando = new ExpandoObject();

    public dynamic Expando
    {
        {
            return _expando;
        }
    }

    void IAttributeAccessor.SetValue(string key, string value)
    {
        _expando[key] = value;
    }

    string IAttributeAccessor.GetValue(string key)
    {
        object value;
        if (_expando.TryGetValue(key, out value) && value != null)
            return value.ToString();
        else
            return null;
    }
}
于 2012-02-26T19:19:42.297 回答