472,337 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 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 2018

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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.