0

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.

Community
  • 1
  • 1
Malavos
  • 429
  • 3
  • 12
  • 27

1 Answers1

1

You are getting this error because a Literal is not a WebControl. They do both inherit from Control however. If you change your code to the following, it should work:

private Control FindAspControlByIdInControl(Control control, string id)
{
    foreach (Control childControl in control.Controls)
    {
        if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase) && childControl is WebControl)
        {
            return childControl;
        }

        if (childControl.HasControls())
        {
            Control result = FindAspControlByIdInControl(childControl, id);
            if (result != null) return result;
        }
    }

    return null;
}
Tim
  • 4,217
  • 1
  • 15
  • 21
  • Thank you, Tim. I will try as you say. I will wait some hours before accepting too see if we reach more answers. – Malavos Feb 18 '15 at 16:45
  • Have just made a slight edit, does that work (sorry am not on dev machine, so no VS to test the code)? – Tim Feb 18 '15 at 16:51