So, I have looked on this question here about searching for a control on server side by ID. I have made the code, and it is as follows:
<asp:Panel CssClass="divMC" id="divTitlesRadios" runat="server" clientidmode="static" >
<ul class="listMesesAluguel">
<asp:Repeater ID="rpetearTitulos" runat="server" OnItemDataBound="rpetearTitulos_ItemDataBound">
<ItemTemplate>
<li class="myListItem" runat="server" id="li1">
<label>
<asp:RadioButton ID="radio1T" runat="server" ClientIDMode="Static" GroupName="radioGroupTitles" OnCheckedChanged="ChangeQTD" AutoPostBack="true" ViewStateMode="Enabled" />
<strong runat="server"><%# Eval("Qtd") %></strong> Title
</label>
<div class="price">
<span class="dosh">R$</span><span runat="server" id="spanValue" class="dosh points"><%# Eval("Dosh") %></span><span class="centsOfDosh"><%# Eval("DoshCents") %></span><span class="spanTime">/monthly</span>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</asp:Panel>
And I have this function here that searches the document:
private WebControl FindAspControlByIdInControl(WebControl control, string id)
{
foreach (WebControl childControl in control.Controls)
{
if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase) && childControl is WebControl)
{
return (WebControl)childControl;
}
if (childControl.HasControls())
{
WebControl result = FindAspControlByIdInControl(childControl, id);
if (result != null) return result;
}
}
return null;
}
I run it in this line:
for (int i = 1; i <= int.Parse(strQtdTitulos.Value); i++)
{
//Div - generic Html control
var rbAux = FindAspControlByIdInControl(divTitulosRadios, "radio" + i);
RadioButton rb = rbAux as RadioButton;
All is well, but into the invoking of this function, it breaks at the line:
foreach (WebControl childControl in control.Controls)
With this error:
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.WebControl'
I already tried replacing it with a div. Can I cast it Somehow? What should I do? I feel I'm a little confused with these type castings.