访问置于转发器标记内的DropDownlist

时间:2014-09-22 16:21:04

标签: c# asp.net .net webforms

下面是标记和代码隐藏。 我试图从代码隐藏中访问标记中的dropdownlist。

 <asp:Repeater runat="server" ID="cataloguesRepeater">  
    <FooterTemplate>
    <table>

     <tbody>
       <tr>   
         <td>
              <asp:DropDownList runat="server" ID="dropDownList1" />
         </td>                       
       </tr>                
     </tbody>
    </table>

    </FooterTemplate>
</asp:Repeater>

代码隐藏

   private void CatalogueItemCommand(object sender, RepeaterCommandEventArgs e)
{
  DropDownList dd1 =(DropDownList)e.Item.FindControl("dropDownList1");
}

由于某种原因,我总是将dd1视为null。如何访问此下拉列表dd1?

1 个答案:

答案 0 :(得分:1)

您必须对ItemCreated事件做出反应:

    protected void Page_Load(object sender, EventArgs e)
    {
        cataloguesRepeater.ItemCreated += cataloguesRepeater_ItemCreated;
        cataloguesRepeater.DataSource =  new [] { new { title = "item1"}, new { title = "item2" } };
        cataloguesRepeater.DataBind();
    }

    void cataloguesRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
    {            
       if (e.Item.ItemType == ListItemType.Footer)
       {
           var ddl = e.Item.FindControl("dropDownList1");
       }            
    }