Search code examples
asp.netrepeater

ASP.NET - How to get selectedvalues from dynamicallyproduced dropdownlists inside Repeater Control


Hi have the following vb code in ASP.NET:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SQL_SRC_DEV_HIERACHY">
    <ItemTemplate>
        <p class="input_title">Hierachy:
            <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Hierarchy_SKey")%>'></asp:Literal><br />
        </p>
        <asp:DropDownList ID="DropDownList_H" runat="server" DataSourceID="SQL_SRC_DEV_GROUPINGDESCS" DataTextField="Description" DataValueField="FTE_PARENT_SKEY"></asp:DropDownList><br />
    </ItemTemplate>
</asp:Repeater>

I now need to get the selectedvalues for each instance of dropdown created, and += to a variable so that I can built a SQL INSERT command based on the selected values.

Can anybody point me in the right direction to achieve this please? Thanks.


Solution

  • You can loop the items in the Repeater in code behind and use FindControl to get the SelectedValue of the DropDownList.

    protected void Button1_Click(object sender, EventArgs e)
    {
        //loop all the items in the repeater
        foreach (RepeaterItem item in Repeater1.Items)
        {
            //use findcontrol to locate the dropdownlist and cast it back to one
            DropDownList drp = item.FindControl("DropDownList_H") as DropDownList;
    
            //display the result
            Label1.Text += drp.SelectedValue;
        }
    }