ASP.Net web forms, .NET 2.0
I have a dilemma. I have an aspx page that has a custom control:
<def:CustomControl ID="customID" runat="server" />
The custom control can be added multiple times to the ASPX page if certain criteria is met:
<asp:Repeater runat="server" ID="options" OnItemDataBound="options_OnItemDataBound">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
</HeaderTemplate>
<ItemTemplate>
<td>
<span>
<asp:Label runat="server" ID="optionName">
</asp:Label>
<asp:DropDownList runat="server" ID="optionValues" CssClass="PartOption" >
</asp:DropDownList>
</span>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
In the code behind of the aspx page, I wanted to append a event (OnSelectedItemChange) to the dropdown control that isn't actually on the ASPX page (yet). However, when I try to do something like the following in the code behind:
foreach(Control eachControl in customID.Controls) {
if (eachControl.HasControls) {
Control DdControl = eachControl.FindControl("optionValues"); //always null
if (DdControl != null && DdControl is typeOf(DropDownList)) {
DdControl = DdControl as DropDownlist; //I might have the syntax wrong
//here, but you get the point.
//add event to control.
}
}
}
But All that eachControl has is a repeater, and it shows its childcontrol count as 0. So when it get to the FindControl method, it always returns null.
QUESTION So what I am trying to do is add an event (OnSelectedItemChange) to the dropdownlist control inside the repeater (which is a custom control that gets added to the page if criteria is met). When a selection is made in the drop down, I want it to trigger the Event method.
How can I either:
A) Find a way to add the event if the dropdownlist control exists on the page?
OR
B) A way to be able to call a method in the code behind and pass the selections from all the drop down lists (since the custom control could be added more than once creating multiple drop downs?
NOTE I don't have the code in front of me, so if I have something syntactically incorrect, please overlook. I also know that ID's are unique, so this is also assumed to be a problem with my logic. When it renders these, the ID's might be mangled by the asp name convention which I would assume I have to find a way around.