0
people〜如何在使用C#動態創建時保留StateManagedCollection?
我正在使用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();
}
}
我的代碼有什麼問題? 有人可以給我任何線索嗎?
你什麼時候動態添加字段?你只是在頁面加載時添加字段並綁定數據,並在回發時忽略數據? – boniestlawyer
@ user1258536我編輯了一些代碼以顯示何時添加列。當它不是回發時,我只添加一列。考茨,有一些領域必須從用戶那裏獲得價值。 –