在搜索上投射literalControl

时间:2015-02-18 15:37:35

标签: c# html asp.net

所以,我已经查看this question here关于在ID上搜索服务器端的控件。我已经编写了代码,它如下:

<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>

我在这里有这个搜索文档的功能:

    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;
    }

我在这行中运行:

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;

一切都很好,但是在调用这个函数时,它会在以下行中断:

    foreach (WebControl childControl in control.Controls)

出现此错误:

  

无法转换类型为#System; Web.UI.LiteralControl&#39;的对象输入   &#39; System.Web.UI.WebControls.WebControl&#39;

我已经尝试用div替换它。我能以某种方式施展它吗?我该怎么办?我觉得我对这些类型的铸件有点困惑。

1 个答案:

答案 0 :(得分:1)

您收到此错误,因为Literal不是WebControl。然而,他们都从Control继承。如果您将代码更改为以下代码,则应该有效:

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;
}