470,639 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,639 developers. It's quick & easy.

ViewState and dynamically created control in repeater

I'm having an odd problem with viewstate and a dynamically created control
inside a repeater template.

Basically, I have a repeater setup like this in the aspx:

<asp:Repeater id="MyRepeater" OnItemDataBound="MyItemDataBound"
<asp:PlaceHolder id="FieldPlaceHolder" runat="server">
<%-- Dynamically created control here. --%>
The code behind uses MyItemDataBound to place a dynamically created control
inside the placeholder. The reason I do that is the dynamically created
control can be one of a number of different types based on the data. Each
of the types derives from the same base control class. Anyway, the item
data bound event method looks something like this:

/// <summary>This is overridden to create the field control.</summary>
protected void MyItemDataBound(object sender, RepeaterItemEventArgs e)
try {
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {
PlaceHolder ph = (PlaceHolder)
MyDataType mdt = (MyDataType) e.Item.DataItem;
MyDataControl mdc = (MyDataControl) Page.LoadControl(mdt.ControlPath);

rqfc.ID = "Field" + mdt.Sequence;
rqfc.CurrentField = mdt;
} catch(Exception ex) {
throw new ApplicationException("Failed loading specific control.", ex);
All the controls loaded from the mdt.ControlPath are ascx controls that
derive from MyDataControl in a separate class file. The bit I'm having the
problem with is this in the MyDataControl file.

/// <summary>This is the base class for all question fields.</summary>
public abstract class MyDataControl : UserControl

/// <value>Gets the field this control is for.</value>
public MyDataType CurrentField {
get {
set {
if(ViewState["Field"] == null || (MyDataType) ViewState["Field"] !=
value) {
ViewState["Field"] = value;
RestoreField(); // abstract function implemented in derived class
protected abstract void RestoreField();
The problem is that the set method in CurrentField, the if always evaluates
to true on post back, because the ViewState["Field"] is always null on post
back in the base class, and the derived classes are executing the
RestoreField method (which I don't want if the same field is being
reloaded). The wierd part is that built in controls in the derived classes
DO HAVE viewstate. One of the derived classes has a asp:TextBox, which
retains it's text value during a post back. It's not clear when in the
lifecycle it's getting it's text set from the viewstate.

I'm always binding the repeater in the OnLoad, so the controls are always
recreated in the same order on post back, so from what I understand of the
docs, the ViewState should come back ok.

Any ideas? (I tried to cut down the code to the relavent bits.... hopefully
I have't cut out the obvious bug else where :)

Nov 18 '05 #1
0 2213

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by John Kirksey | last post: by
reply views Thread by Moheb Missaghi | last post: by
reply views Thread by John Crowley | last post: by
1 post views Thread by olduncleamos | last post: by
1 post views Thread by Hardy Wang | last post: by
6 posts views Thread by hitendra15 | last post: by
1 post views Thread by Arpan | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.