How to handle Drop-Down event in Repeater control


In my project I have to handle the event of drop-down control in repeater control and also want the current item index. I get how to handle the event but I need to burn my midnight's oil to get the current item index and finally I got the solution using NamingContainer property. 

Following is the sample code for repeater control and drop-down control in it

<asp:Repeater ID="myRepeater" runat="server">
 <ItemTemplate>
  <asp:DropDownList ID="myDropdown" runat="server" AppendDataBoundItems="true"    AutoPostBack="true" OnSelectedIndexChanged="myDropdown_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
 </asp:Repeater>

Added the event myDropdown_SelectedIndexChanged to the code behind as follows : 

protected void myDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the drop down instance as follows
    DropDownList myDropdown = sender as DropDownList;
    // Get the current item index
    int currentItemIndex = ((RepeaterItem)drpVariants.NamingContainer).ItemIndex;
}

Here we are getting the RepeaterItem using the Namingcontainer property. Here Repeater control is naming container. i.e. - it provides unique ids to its child controls. Here is the very good blog on NamingContainer.


Similar Articles