1

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.

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
CodeRedick
  • 7,346
  • 7
  • 46
  • 72
  • I think, What you are doing is, you have formview on the page and formview bind to LinqDatasource, you have some custom/additional field that you want to populate in formview and when you want to save/update record, you need custom/additinal field Values????? – Muhammad Akhtar Jun 16 '09 at 12:10
  • Can you show me your code, how you are adding Custom Template? – Muhammad Akhtar Jun 16 '09 at 12:22
  • There is a FormView on the main page, the Dynamic Data templates also include FormViews with their own LinqDataSource. I'm not directly adding any Custom Templates, they are being added by the ASP.NET Dynamic Data functionality... I'm just setting UIHint on the metadata classes... the code is a bit complex to post, but if I need to I probably can later today. – CodeRedick Jun 17 '09 at 15:21
  • What you are doing is, you are adding custom field and binding them is creating problem, you have to add these custom field in other way, Plz check my answer – Muhammad Akhtar Jun 19 '09 at 12:04
  • have u resolve the issue........ – Muhammad Akhtar Jul 07 '09 at 05:29
  • Handling the exception seems to be safe. I figured out the actual _cause_ is using FindControl to search for other controls on the form. That triggers the page_load event before the Dynamic Data stuff is actually ready for it... – CodeRedick Jul 07 '09 at 12:47

2 Answers2

0

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
  • 20,542
  • 15
  • 71
  • 88
  • 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. – CodeRedick Jun 17 '09 at 17:14
0

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.

NotMe
  • 87,343
  • 27
  • 171
  • 245