如何在使用C#动态创建时保持StateManagedCollection?
人~ 我正在使用C#创建一个从CompositeDataBoundControl继承的自定义gridview。 我有一个公共财产,称为“列”,如下所示如何在使用C#动态创建时保持StateManagedCollection?,c#,custom-controls,custom-server-controls,state-management,C#,Custom Controls,Custom Server Controls,State Management,人~ 我正在使用C#创建一个从CompositeDataBoundControl继承的自定义gridview。 我有一个公共财产,称为“列”,如下所示 private FixedGridColumnCollection _columnCollection = null; /// <summary> /// FixedGrid's columns /// </summary> [Category("BANANA Framework")
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;
}
}
我的代码怎么了?
有人能给我一些线索吗?如果在页面初始化中加载列,这些列应该能够足够早地获取视图状态,并且所有列都应该每次呈现。仅在第一页请求时加载数据。大概是这样的:
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();
}
}
何时动态添加字段?你只是在页面加载时添加字段并绑定数据,而在回发时忽略吗?@user1258536我编辑了一些代码,以便在添加列时显示。我只在不回发时添加一次列。因为,有些字段必须从用户处获取值。
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();
}
}
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();
}
}