473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2087

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

Similar topics

9
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.