473,505 Members | 13,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"
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 :)

Nov 18 '05 #1
0 2288

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
21627
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
0
1418
by: Moheb Missaghi | last post by:
Hi: I am trying to use the Transfer statement in an .aspx file to redirect and send Form and QueryString collections to a different page. A good example where this is needed is a checkout page...
8
4264
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
0
2098
by: John Crowley | last post by:
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:
1
5401
by: olduncleamos | last post by:
Hello all, I am experimenting with the repeater control and ran into something that I wasn't expecting. I would appreciate if the experts can confirm or correct my understanding. Here is a...
1
1417
by: Hardy Wang | last post by:
Hi all, I have a main ASPX page, and a control ASCX page, there is a Repeater control in ASCX page, and several drop down boxes in main page. These drop down boxes will trigger postback. In the...
6
2837
by: hlubocky | last post by:
I thought I had a good grasp of the problem related to dynamically creating controls, but it appears that as my application grew in complexity, the problem has resurfaced. As I understand it, in...
6
2058
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
1
1493
by: Arpan | last post by:
The following code retrieves records from a database table & displays it in a Repeater control: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not...
0
1870
by: Dave A | last post by:
I have a user control that is dynamically loaded into a repeater; (so the user control appears several times) The user control features a text box and a delete button. When the delete button...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.