2014-09-22 30 views
1

下面是標記和代碼隱藏。 我想從代碼隱藏中訪問標記中的下拉列表。訪問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爲空的某些原因。如何訪問此dropdownlist dd1?

回答

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"); 
     }    
    } 
+0

謝謝你的作品非常好 – Sike12 2014-09-23 11:33:48