2008-11-27 82 views
1

我在Repeater控件上有一個DropDownList,還有一個按鈕。從Repeater檢索同級控件事件

當我想啓用按鈕時,該按鈕將被禁用,直到在DropDownList上選擇了一個有效的項目。不幸的是,我似乎無法做到。

發現人轉發:(。至於()方法是(對象T)的擴展方法,只是讓鑄造更容易)

sender.As<Control>().NamingContainer.Parent.As<Repeater>() 

但是我回來直放站不幫我FindControl(字符串名稱)函數不會返回任何內容 - 並且在監視窗口中沒有任何用處。

那麼,如何從中繼器上的另一個項目的事件(本例中爲DropDown_SelectedIndexChanged)在中繼器上獲得兄弟控件(在此情況下爲ImageButton)?

編輯

我終於摸索出

sender.As<ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName") 

回答

4

我想我已經爲您的問題的答案:

1,我創建一個具有下拉列表和按鈕轉發器做測試:

<asp:Repeater ID="rp" runat="server"> 
    <ItemTemplate> 
     <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
     <asp:ListItem>3</asp:ListItem> 
     <asp:ListItem>4</asp:ListItem> 
     <asp:ListItem>5</asp:ListItem> 
     <asp:ListItem>6</asp:ListItem> 

     </asp:DropDownList> 
     <asp:ImageButton ID="Button1" runat="server" Enabled="False" /> 
     </ItemTemplate> 
     </asp:Repeater> 

我數據綁定中繼器。

2-I創建方法DropDownList1_SelectedIndexChanged:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList control = (DropDownList)sender; 

     RepeaterItem rpItem = control.NamingContainer as RepeaterItem; 
     if (rpItem != null) 
     { 
      ImageButton btn = ((ImageButton)rpItem.FindControl("Button1")); 
      btn.Enabled = true; 

     } 

    } 

做到這一點是要求控制的方式,誰是它的母公司,這是說,的RepeaterItem,或者您可以使用NamingContainer(正如我最後寫的),在那裏你可以詢問裏面的任何控制。

+0

我終於找出sender.As ()。NamingContainer.As ()。FindControl(「ControlName」) – johnc 2008-11-27 21:03:44