Вопрос

people~

I am creating a custom gridview inherits from CompositeDataBoundControl using C#. I have a public property, called "Columns" as below.

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

Now that FixedGridColumnCollection class is below.

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

It works fine with a page when columns are declared statically. But when I create columns dynamically, I lose all columns when postback like below.

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

What's wrong with my code? Can somebody give me any clue, please?

Это было полезно?

Решение

If you load the columns in the page initialisation the columns should be able to pick up view state early enough and all columns should render every time. With your data only loading on first page request. Something like this:

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();
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top