C
Christophe Peillet
I have a series of composite controls to replace most common form controls
(adding Ajax support and a built in validator), and have all of the simple
controls working (textbox, checkbox, button, etc.), but have an issue with
Viewstate and Postback when it comes to ListItem based controls
(CheckboxList, DropDownList and RadioButtonList).
I need to set the Item properties value in Viewstate, but they do not seem
to persist on postback when I do this (all of the other properties do except
this one, so it isn't, I don't think, a problem like unique ID, etc.).
When I set the collection directly to the underlying child control, it works
propertly and persists across viewstates (see first example below), but,
because of other restrictions in my control, I am unable to do this, and NEED
to keep this information in Viewstate, and reassign the appropriate value to
m_chk after a postback.
This is what I have at the moment, which maintains data across postback, but
doesn't work in my case since I need to keep it in Viewstate and manually
reassign it to m_chk.
/// <summary>
/// Gets the CheckBoxList items.
/// </summary>
/// <value>The CheckBoxList items.</value>
[Bindable(true)]
[DefaultValue((string)null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
[MergableProperty(false)]
[Category("CheckBox")]
public ListItemCollection Items
{
get
{
EnsureChildControls();
return m_chk.Items;
}
}
When I try this, it adds everything to ViewState, but the contents are
always lost on postback.
/// <summary>
/// Gets the CheckBoxList items.
/// </summary>
/// <value>The CheckBoxList items.</value>
[Bindable(true)]
[DefaultValue((string)null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
[MergableProperty(false)]
[Category("CheckBox")]
public ListItemCollection Items
{
get
{
ListItemCollection l = (ListItemCollection)Viewstate["CheckboxListItems"];
return l;
}
}
How can I persist this information across postbacks, but assigning it to
viewstate.
Thanks for any help on this. (Someone should real write a good book on
custom component development for ASP using real world development examples
(inheritance, etc.) ... there are surprisingly few helpful resources out
there.)
(adding Ajax support and a built in validator), and have all of the simple
controls working (textbox, checkbox, button, etc.), but have an issue with
Viewstate and Postback when it comes to ListItem based controls
(CheckboxList, DropDownList and RadioButtonList).
I need to set the Item properties value in Viewstate, but they do not seem
to persist on postback when I do this (all of the other properties do except
this one, so it isn't, I don't think, a problem like unique ID, etc.).
When I set the collection directly to the underlying child control, it works
propertly and persists across viewstates (see first example below), but,
because of other restrictions in my control, I am unable to do this, and NEED
to keep this information in Viewstate, and reassign the appropriate value to
m_chk after a postback.
This is what I have at the moment, which maintains data across postback, but
doesn't work in my case since I need to keep it in Viewstate and manually
reassign it to m_chk.
/// <summary>
/// Gets the CheckBoxList items.
/// </summary>
/// <value>The CheckBoxList items.</value>
[Bindable(true)]
[DefaultValue((string)null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
[MergableProperty(false)]
[Category("CheckBox")]
public ListItemCollection Items
{
get
{
EnsureChildControls();
return m_chk.Items;
}
}
When I try this, it adds everything to ViewState, but the contents are
always lost on postback.
/// <summary>
/// Gets the CheckBoxList items.
/// </summary>
/// <value>The CheckBoxList items.</value>
[Bindable(true)]
[DefaultValue((string)null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
[MergableProperty(false)]
[Category("CheckBox")]
public ListItemCollection Items
{
get
{
ListItemCollection l = (ListItemCollection)Viewstate["CheckboxListItems"];
return l;
}
}
How can I persist this information across postbacks, but assigning it to
viewstate.
Thanks for any help on this. (Someone should real write a good book on
custom component development for ASP using real world development examples
(inheritance, etc.) ... there are surprisingly few helpful resources out
there.)