Alex Skiba
Alex Skiba

Reputation: 437

How to set initial SelectedItem in DropDownList inside ListView EditItemTemplate?

I have a ListView control with DDL in its EditItemTemplate:

<asp:ListView ID="ListView1" ItemType="Project.Models.Product" 
              SelectMethod="GetProducts" runat="server" >
    <EditItemTemplate>
        <asp:DropDownList runat="server" ID="ProducerDDL" 
                          DataValueField="ProducerId" 
                          DataTextField="Name" 
                          SelectMethod="GetProducers" />
    </EditItemTemplate>
</asp:ListView>

Project.Models.Product contains property Producer. So the question is: how to set actual Producer of the edited Product item as the selected item of ProducerDDL?

Upvotes: 1

Views: 2082

Answers (1)

Alex Skiba
Alex Skiba

Reputation: 437

So it seems the solution is just using Bind() the proper way:

<asp:DropDownList SelectedValue='<%# Bind("Producer.ProducerId") %>' />

as the binding context is my Product item.

Upvotes: 2

Related Questions