如何使用C#动态创建StateManagedCollection?

时间:2013-06-25 02:57:40

标签: c# custom-controls custom-server-controls state-management

人〜

我正在使用C#创建一个自定义gridview继承自CompositeDataBoundControl。 我有一个公共财产,名为“Columns”,如下所示。

    private FixedGridColumnCollection _columnCollection = null;
    /// <summary>
    /// FixedGrid's columns
    /// </summary>
    [Category("BANANA Framework")]
    [Description("Gets or sets FixedGrid's columns")]
    [Editor(typeof(FixedGridColumnCollectionEditor), typeof(UITypeEditor))]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [ReadOnly(true)]
    public FixedGridColumnCollection Columns
    {
        get
        {
            if (this._columnCollection == null)
                this._columnCollection = new FixedGridColumnCollection();
            if (base.IsTrackingViewState)
            {
                ((IStateManager)(this._columnCollection)).TrackViewState();
            }
            return _columnCollection;
        }
    }

现在FixedGridColumnCollection类在下面。

[TypeConverter(typeof(FixedGridColumnConverter))]
public class FixedGridColumnCollection : StateManagedCollection
{
    // Fields
    #region Type : FixedGrid's column types
    /// <summary>
    /// FixedGrid's column types
    /// </summary>
    private static readonly Type[] _knownTypes = new Type[]
    {
        typeof(BANANA.Web.Controls.BoundDataField)
        , typeof(BANANA.Web.Controls.TextBoxField)
        , typeof(BANANA.Web.Controls.HyperLinkField)
        , typeof(BANANA.Web.Controls.LinkButtonField)
        , typeof(BANANA.Web.Controls.CheckBoxField)
        , typeof(BANANA.Web.Controls.DropDownListField)
        , typeof(BANANA.Web.Controls.RadioButtonField)
        , typeof(BANANA.Web.Controls.DatePickerField)
        , typeof(BANANA.Web.Controls.CodeHelperField)
        , typeof(BANANA.Web.Controls.TemplateField)
    };
    #endregion

    // Properties
    #region BoundFieldBase
    public BaseDataField this[int index]
    {
        get { return (BaseDataField)((IList)this)[index]; }
    }
    #endregion

    #region Type
    protected override Type[] GetKnownTypes()
    {
        return _knownTypes;
    }
    #endregion

    // Methods
    #region Add
    public int Add(BaseDataField item)
    {
        return ((IList)this).Add(item);
    }
    #endregion

    #region Remove
    public void Remove(BaseDataField item)
    {
        ((IList)this).Remove(item);
    }
    #endregion

    #region SetDirtyObject
    protected override void SetDirtyObject(object o)
    {
        ((BaseDataField)o).SetDirty();
    }
    #endregion

    #region CreateKnownType
    protected override object CreateKnownType(int index)
    {
        switch (index)
        {
            case 0:
                return new BANANA.Web.Controls.BoundDataField();
            case 1:
                return new BANANA.Web.Controls.TextBoxField();
            case 2:
                return new BANANA.Web.Controls.HyperLinkField();
            case 3:
                return new BANANA.Web.Controls.LinkButtonField();
            case 4:
                return new BANANA.Web.Controls.CheckBoxField();
            case 5:
                return new BANANA.Web.Controls.DropDownListField();
            case 6:
                return new BANANA.Web.Controls.RadioButtonField();
            case 7:
                return new BANANA.Web.Controls.DatePickerField();
            case 8:
                return new BANANA.Web.Controls.CodeHelperField();
            case 9:
                return new BANANA.Web.Controls.TemplateField();
            default:
                throw new Exception("Does not support this kind of filed in FixedGrid.");
        }
    }
    #endregion

    #region OnValidate
    protected override void OnValidate(object o)
    {
        base.OnValidate(o);
        if (!(o is BaseDataField))
            throw new ArgumentException("It must be value of BaseDataField.", "value");
    }
    #endregion
}

当静态声明列时,它适用于页面。 但是当我动态创建列时,我会在回发时丢失所有列,如下所示。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BANANA.Web.Controls.BoundDataField field = null;
            BANANA.Web.Controls.TemplateField tField = null;

            tField = new BANANA.Web.Controls.TemplateField();
            tField.ItemTemplate = new GridViewRadioButtonTemplate("APPSTATUS", _strRadioButtonID);
            tField.ID = "APPSTATUS";
            tField.Width = 30;
            tField.HorizontalAlignment = BANANA.Web.Controls.HorizontalAlignment.Center;
            this.FixedGrid1.Columns.Add(tField);

            FixedGrid1.DataSource = _dt;
            FixedGrid1.DataBind();
        }
    }

我的代码出了什么问题? 请问有人能给我任何线索吗?

1 个答案:

答案 0 :(得分:0)

如果在页面初始化中加载列,则列应该能够足够早地获取视图状态,并且每次都应该呈现所有列。您的数据仅在首页请求时加载。像这样:

protected void Page_Init(object sender, EventArgs e)
{
    BANANA.Web.Controls.BoundDataField field = null;
    BANANA.Web.Controls.TemplateField tField = null;

    tField = new BANANA.Web.Controls.TemplateField();
    tField.ItemTemplate = new GridViewRadioButtonTemplate("APPSTATUS", _strRadioButtonID);
    tField.ID = "APPSTATUS";
    tField.Width = 30;
    tField.HorizontalAlignment = BANANA.Web.Controls.HorizontalAlignment.Center;
    this.FixedGrid1.Columns.Add(tField);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FixedGrid1.DataSource = _dt;
        FixedGrid1.DataBind();
    }
}