I'm looking for a work around to this annoyance.
The Setup.
You have a base page, with a protected visual element on it. Call it a DataGrid named "MyDatagrid". So in your base page you'll have a protected DataGrid. It stands to reason that every page that inherits from this base is gong to have a DataGrid named "MyDatagrid" in the ASPX file.
The Annoyance.
Every time you open the derived page, "protected DataGrid MyDatagrid;" is added to the derived code behind file. This compiles, but of course throws a runtime error.
The Requested solution.
Any way to step from the base, to the code behind, to the aspx, so the designer is happy, the derived code behind is happy, and the base page is happy?
This is amazing to me, that I can't find anything on the net about this. I'm really hoping that it's me doing something wrong, but I've now wasted over 2 hours, so I'm asking fro help.
I have a simple FindControl routine, that finds a control on a Controls collection. If it's not there, it looks at the child controls recursively.
This has worked for years.
BUT... For the first time I'm using a CheckBoxList control (native to .net) and when my code comes across this control, it's FindControl method will return a CheckBoxList, for any key that is passed in. Here is some text form the immediate window. "container" is a CheckBoxList.
1: container.FindControl("asdf")
2: {System.Web.UI.WebControls.CheckBoxList}
3: System.Web.UI.WebControls.ListControl: {System.Web.UI.WebControls.CheckBoxList}
4: CellPadding: -1
5: CellSpacing: -1
6: controlToRepeat: {Text="" Checked=false}
7: hasNotifiedOfChange: false
8: RepeatColumns: 0
9: RepeatDirection: Vertical
10: RepeatLayout: Table
11: TextAlign: Right There is obviously not a control named "asdf". Why the heck is this returning a control?
Thanks for your time in helping me research this problem.
UPDATE :: Aug - 4 - 2005
As the comments say, if you use Reflector, you see the CheckBoxList.FindControl() always retruns an instance of it self.
So if you're writing a FindControl recursive method, like the following ...
Control control = container.FindControl(id);
if (control != null) return control;
You'll need to modify and add the code in red.
Control control = container.FindControl(id);
if (control != null && control.id == id) return control;
This will ensure that the control your looking for is the one that get's returned.