DropDownList的内部中继器:如何​​处理SelectedIndexChange并获得DataItem的?中继器、DropDownList、DataItem、SelectedIndexChan

2023-09-03 04:04:42 作者:惡魔の承諾

我把一个DropDownList用的AutoPostBack一个中继器内。 (该ListItems的填充上转发的的ItemDataBound)

 < ASP:直放站ID =rptWishlistOnItemCommand =rptWishlist_ItemCommandonItemDataBound =rptWishlist_ItemDataBound=服务器>
  <的ItemTemplate>
    ...
    < ASP:DropDownList的ID =ddlSize=服务器的AutoPostBack =真正的OnSelectedIndexChanged =ddlSize_SelectedIndexChanged/>
    ...
 

首先,这一功能甚至没有开枪后回

保护无效ddlSize_SelectedIndexChanged(对象发件人,EventArgs五) { //这个函数永远不会被调用 }

我怎么会那么得到的DataItem后,我得到它的工作?

难道我这做了错误的方式?

感谢你在前进。

解决方案

要注册的DropDownList的回传,添加以下code:

 受保护的虚拟无效RepeaterItemCreated(对象发件人,RepeaterItemEventArgs E)
    {
        DropDownList的MYLIST =(DropDownList的)e.Item.FindControl(ddlSize);
        MyList.SelectedIndexChanged + = ddlSize_SelectedIndexChanged;
    }
 

而在你的aspx文件,添加到您的中继标记:

  OnItemCreated =RepeaterItemCreated
 

然后,在你ddlSize_SelectedIndexChanged功能,可以访问父控制是这样的:

  DropDownList的D =(DropDownList的)发送;
   (的RepeaterItem)d.Parent ...
 

希望这有助于。

I am putting a DropDownList with AutoPostBack inside a Repeater. (The ListItems are populated on the repeater's ItemDataBound)

<asp:Repeater ID="rptWishlist" OnItemCommand="rptWishlist_ItemCommand" onItemDataBound="rptWishlist_ItemDataBound" runat="server">
  <ItemTemplate>
    ...
    <asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSize_SelectedIndexChanged" />
    ...

Firstly, this function was not even fired on post back

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

How would I then get the DataItem after I get it working?

Am I doing this the wrong way?

Thank you in advance.

解决方案

To register the dropdownlist for postback, add the following code:

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

And in your aspx file, add this to your repeater markup:

OnItemCreated="RepeaterItemCreated"

Then, in your ddlSize_SelectedIndexChanged function, you can access the parent control like this:

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

Hope this helps.