ViewState and dynamically created control in repeater

J

John Crowley

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"
runat="server">
<ItemTemplate>
<asp:placeHolder id="FieldPlaceHolder" runat="server">
<%-- Dynamically created control here. --%>
</asp:placeHolder>
</ItemTemplate>
</asp:Repeater>


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)
e.Item.FindControl("FieldPlaceHolder");
MyDataType mdt = (MyDataType) e.Item.DataItem;
MyDataControl mdc = (MyDataControl) Page.LoadControl(mdt.ControlPath);

rqfc.ID = "Field" + mdt.Sequence;
ph.Controls.Add(rqfc);
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 {
return(ViewState["Field"]);
}
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top