2016-04-25 148 views
0

我有兩個自定義控件。我有一個Selector,它繼承了CompositeControl,還有一個從DropDownList繼承的下拉列表。下面是我的aspx頁面的代碼片段:ASP.net Web窗體自定義控件自動更改下一個控件的ID

<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爲我的第二控制。我怎樣才能找到原因是什麼?

+0

請爲您的選擇器控件提供代碼 – techspider

+0

@techspider延遲的道歉。我終於找到了解決方案。我發佈了下面的代碼。 –

回答

0

我終於找到了解決辦法!問題是,我以前使用的AddAttributesToRender()我RenderContents()內:

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

     string htmlOutput = getHtmlOutput(); 

     output.Write(htmlOutput); 
    } 

一旦我刪除的AddAttributesToRender(),重複的ID問題就消失了。