2012-03-01 137 views
6

我在Repeater裏放了一個帶AutoPostBack的DropDownList。
(該listItems中被填充中繼器上的的ItemDataBound)Repeater內的DropDownList:如何處理SelectedIndexChange並獲取DataItem?

<asp:Repeater ID="rptWishlist" OnItemCommand="rptWishlist_ItemCommand" onItemDataBound="rptWishlist_ItemDataBound" runat="server"> 
    <ItemTemplate> 
    ... 
    <asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSize_SelectedIndexChanged" /> 
    ... 
  1. 首先,這一功能甚至沒有開槍後回

    protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
    {
    //This function is never called
    }

  2. 我如何在獲得數據項後獲取DataItem?

上午我這個做了錯誤的方式?

預先感謝您。

回答

12

要註冊的下拉列表的回傳,添加以下代碼:

protected virtual void RepeaterItemCreated(object sender, RepeaterItemEventArgs e) 
    { 
     DropDownList MyList = (DropDownList)e.Item.FindControl("ddlSize"); 
     MyList.SelectedIndexChanged += ddlSize_SelectedIndexChanged; 
    } 

而在你的aspx文件,添加到您的中繼標記:

OnItemCreated="RepeaterItemCreated" 

然後,在你ddlSize_SelectedIndexChanged功能,你可以像這樣訪問父控件:

DropDownList d = (DropDownList)sender; 
    (RepeaterItem) d.Parent... 

希望這個hel PS。

+0

非常感謝!那我該如何獲取DropDownList所屬的DataItem? – Aximili 2012-03-01 03:28:52

+1

DataItem僅在ItemCreated和ItemDataBound方法中可用。我認爲你必須做的是增加一些控制到你可以用來引用你想要檢索的項目的中繼器。 – jmaglio 2012-03-01 03:59:11

+0

爲什麼不能'OnSelectedIndexChanged =「ddlSize_SelectedIndexChanged」'? – toddmo 2016-04-07 20:18:42

1

如果你只是想火OnSelectedIndexChanged,這是應該的樣子:

Page.aspx - 源

<FooterTemplate> 
    <asp:DropDownList ID="ddlOptions" 
      runat="server" 
      AutoPostBack="true" 
      onselectedindexchanged="ddlOptions_SelectedIndexChanged"> 
     <asp:ListItem>Option1</asp:ListItem> 
     <asp:ListItem>Option2</asp:ListItem> 
    </asp:DropDownList> 
</FooterTemplate> 

Page.aspx.cs - 代碼隱藏

protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //Event Code here. 
    } 

就是這樣。你的活動現在將被召喚。

2

我看到您發佈的代碼部分沒有問題。

當IsPostBack爲true並且在PageLoad期間,您是否在中繼器上調用了DataBind()? 如果是的話,你將失去的SelectedIndexChanged你的DDL

,可以儲存的ID,例如值或HiddenField,回發處理過程中加載特定DataItems(ListView中有DataKey用於此目的)

作爲一般指南:

  • 它往往是更好的PreRender期間調用DataBind()
  • 如果基礎數據並沒有改變,你不應該回發期間調用DataBind()
  • 如果你做上面的兩點,你將無法在 使用DataItems item_created(如您的DataItems將只有當你調用可用 的DataBind())

    protected void Page_Load(object sender, EventArgs e) 
    { 
        this.PreRender += new EventHandler(test_PreRender); 
    } 
    
    void test_PreRender(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
         rptWishlist.DataSource = new int[] { 1, 2, 3, 4 }; 
         rptWishlist.DataBind(); 
        } 
    } 
    
    protected void rptWishlist_ItemCommand(object sender, RepeaterCommandEventArgs e) 
    { 
        //Command Code Here 
    } 
    
    protected void rptWishlist_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
        var i = (int) e.Item.DataItem; 
        var ddl = (DropDownList)e.Item.FindControl("ddlSize"); 
        for(int j=1; j<=i;j++) 
        { 
         ddl.Items.Add(new ListItem(){Text = j.ToString()}); 
    
        } 
    } 
    
    protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        Response.Write("changed"); 
    } 
    
1

答案在這裏很好,但缺少至關重要的檢查。如果您想知道爲什麼您要將對象引用設置爲對象錯誤的實例,請務必注意,中繼器將在任何數據項之前首先創建其HEADER。

執行此項檢查:

protected void rptProjects_ItemCreated(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
      ((DropDownList)e.Item.FindControl("yourcontrol")).SelectedIndexChanged += ddlAction_SelectedIndexChanged; 
    } 
} 
-1

試試這個

DropDownList drp = sender as DropDownList; 
    int RepeaterItemIndex = ((RepeaterItem)drp.NamingContainer).ItemIndex; 
相關問題