2

I'm working on a project where the page load certain controls depending on the index available. The loading occurs in the page load where the method PopulateSearchField is called.

Within this method, all the UserControl are added on the page using : Page.LoadControl("path");

The page load and all the required controls are on the page. My problem is when the user click on the Search button the event is triggered and a query is built based on the user input int those controls. Unfortunately, the method isn't able to produce a proper query as it is unable to find any of the controls on the page.

With a temporary ControlCollection variable, I've been able to see that the number of controls on my page is 3 when it should be something from 4 to 10. Those 3 controls in the collection are the static label and buttons on the page.

I don't know if something is wrong with the code or if it's a page cycle problem as this solution used to work on framework 1.1. Yeah, I know this isn't the best thing to do so, but they did it this way and I gotta make it work.

I'm not sure if it is the migration that has caused the problem or not.

Thanks a lot, David!

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
David
  • 1,101
  • 5
  • 19
  • 38
  • UPDATE : At least at the end of the Page_Load or the PreInit depending on where I use the PopulateSearchField method, I can reach the Page.Controls collection and all the dynamically added controls are there. It is only on the Search_Button.click() that those controls aren't there anymore. Maybe it can help you. – David Mar 25 '11 at 11:54

2 Answers2

1

When you click the button, the controls are no longer available server side when your click handler is being processed. The page, server side, has no knowledge of the controls you created dynamically since there are no server side controls for the posted values to map to. If you want to find the values, you need to inspect the posted control data and not rely on the server side asp.net control heirarchy.

You could also write all the data you require to a hidden field via javascript and then read the hidden data server side since it will will be posted.

The following is occuring:

  1. Creating controls dynamically
  2. Posting controls data on click
  3. ASP.NET maps the data to the existing controls it knows about.
  4. Your controls are not found so the data is no mapped to anything.

You need to add your controls before the mapping occurs (in PreInit). Check out the Page Lifecycle and you will see how it ties all the controls and data together.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • Thanks a lot, I'll give it a shot, I'll try to add thoses controls before the preinit to be sure that the controls are mapped. – David Mar 25 '11 at 11:47
  • It seems like adding the controls in the PreInit instead of the Page_Load didn't work either. There's probably something I'm not catching haha. I'll look all over the Page Life Cycle to find out. – David Mar 25 '11 at 11:48
  • I just did some debugging and it tells me two things. At the end of the OnPreInit(), my Page.Controls collection has 8 elements, it means the dynamic search fields have been added correctly. But if I place a breakpoint at the Page_Load, the Page.Controls now only have 3 elements. This is weird, in the same cycle my controls are gone. – David Mar 25 '11 at 11:58
  • Since I used to check if(!IsPostBack) before to add the controls, the controls weren't "readded" upon reload. Thanks a lot! :) – David Mar 25 '11 at 12:09
0

Are you re-adding the controls to the new page when the user clicks search?

Remember... every time the user hits your server for that page... a new Page object is created. If you're dynamically adding controls, you have to do it every time the page loads.

Additionally, since you seem to want to get values out of the controls, you're going to have to make sure that the controls are created with the exact same ID property every time, and created before viewstate is loaded, if you want them to retain their values.

womp
  • 115,835
  • 26
  • 236
  • 269
  • The controls have the same id, that isn't the problem. If I could at least see those controls in the Page.Controls ControlCollection, I'd be easy then to get the values, but the controls aren't even part of the collection. – David Mar 25 '11 at 11:50
  • Thanks too Whomp :), now my problem is an ID problem hehe. Now what you used is being very useful! – David Mar 25 '11 at 12:10