views:

407

answers:

3

This one is a little... odd. Basically I have a form I'm building using ASP.NET Dynamic Data, which is going to utilize several custom field templates.

I've just added another field to the FormView, with it's own custom template, and the form is loading that control twice for no apparent reason. Worse yet, the first time it loads the template, the Row is not ready yet and I get the error message:

{"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."}

I'm accessing the Row variable in a LinqDataSource OnSelected event in order to get the child object...

Now for the wierd part: If I reorder the fields a little, the one causing the problem no longer gets loaded twice.

Any thoughts?

EDIT: I've noticed that Page_Load gets called on the first load (when Row throws an exception if you try to use it) but does NOT get called the second time around. If that helps any...

Right now managing it by just catching and ignoring the exception, but still a little worried that things will break if I don't find the real cause.

EDIT 2: I've traced the problem to using FindControl recursively to find other controls on the page. Apparently FindControl can cause the page lifecycle events (at least up to page_load) to fire... and this occurs before that page "should" be loading so it's dynamic data "stuff" isn't ready yet.

A: 

You aren't referring to something in Page_Load that is set in OnSelected, are you? Something like your row object. OnSelected is a postback event and occurs after Page_Load. It's the only thing I can think of that might cause your exception when the row is not ready.

Steve Cooper
No, nothing is actually being done in Page_Load... I only even had the function in there so I could set a breakpoint and see what's happening. Moreover the Row object is generated somewhere in the dynamic data stuff... not anywhere in my code, so one would think it gets generated before it tries loading the FieldTemplate, since the FieldTemplate depends on it.
Telos
A: 

Start commenting things out in your code behind until either everything is commented or your control no longer loads twice.

If it is still doing it, start unhooking things in the control itself. Eventually, you'll get to the actual issue.

Chris Lively
A: 
Muhammad Akhtar
Adding controls in code and managing view state is exactly what I am trying to get away from using ASP.NET Dynamic Data. Why does it sound like your solution is using plain old form tags instead of asp:DynamicControl tags?
Telos
Actually we bound to follow application architecture of our company and that’s not upgraded yet, what we are following is, we have typed Dataset in our DAL, in the BLL we have Entity classes can be called as wrapper classes. On the presentation layer we use formView/DetailsView that will bind to objectdatasource. ...
Muhammad Akhtar