【问题标题】:ASP.net Web Forms Custom control automatically changes the ID of next controlASP.net Web Forms 自定义控件自动更改下一个控件的 ID
【发布时间】:2016-04-25 17:08:18
【问题描述】:

我有两个自定义控件。我有一个继承 CompositeControl 的 Selector 和一个继承自 DropDownList 的下拉菜单。下面是我的 aspx 页面中的代码 sn-p:

    <table border="0" cellpadding="0" cellspacing="3" width="100%">                         
        <tr>
            <td class="a12bold" style="padding: 0px 0px 5px 0px;">contact:</td>
            <td>
                <div><cc:Selector id="SelectorID" runat="server" Width="300" /> </div>                                
            </td>
        </tr>
    </table>
    <table border="0" cellpadding="0" cellspacing="3" width="100%"> 
        <tr>
            <td class="a12bold" style="padding: 0px 0px 5px 0px;">Category:</td>
            <td><cm:DropDown id="dropDownID" runat="server"   Width="350px" AutoSelectSingleRow="true"></cm:DropDown></td>
        </tr>
    </table>

由于某种原因,当我的选择器被渲染时,第二个自定义控件最终有两个 ID。下面是第二个控件的效果图:

        <td><select id="SelectorID" name="dropDownID" id="dropDownID" style="width:350px;">
    <option selected="selected" value=""></option>

我花了两天时间试图解决这个问题。到目前为止我做过的事情:

  1. 为我的选择器调试 RenderContents() 覆盖
  2. 为我的下拉菜单调试 RenderContent() 覆盖。
  3. 检查 HTML 语法错误。 (开始标签,结束标签)

在渲染之前,服务器端的一切似乎都运行良好,它为我的第二个控件放置了两个 ID。我怎样才能找到原因?

【问题讨论】:

  • 请为您的选择器控件提供代码
  • @techspider 为延迟道歉。我终于找到了解决方案。我发布了下面的代码。

标签: asp.net custom-controls


【解决方案1】:

我终于找到了解决方案!问题是我之前在 RenderContents() 中使用了 AddAttributesToRender():

    protected override void RenderContents(HtmlTextWriter output)
    {
        EnsureChildControls();
        AddAttributesToRender();

        string htmlOutput = getHtmlOutput();

        output.Write(htmlOutput);
    }

删除 AddAttributesToRender() 后,重复 ID 问题就消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2010-09-23
    • 2011-06-15
    • 2011-02-21
    相关资源
    最近更新 更多