Question

I have a couple of straightforward dropdownlists. The first is bound by a linqdatsource in the html with autopostback set to true. The selection of the first determines the data in the second. When I select an item in the first, the selectedindexChanged event fires; however, the selected value is always the first item in the list and the list then re-binds and reverts to its default state. Do I have to bind it in code-behind to prevent this?

<asp:DropDownList ID="dd_shirtcolor" runat="server" AppendDataBoundItems="true" AutoPostBack="True">
    <asp:ListItem Text="Select Color" />
</asp:DropDownList>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
    ContextTypeName="IPC.IPCDataDataContext" EntityTypeName=""
    TableName="Shirts" Where="IsActive == @IsActive">
    <WhereParameters>
        <asp:Parameter DefaultValue="true" Name="IsActive" Type="Boolean" />
    </WhereParameters>
</asp:LinqDataSource>
Was it helpful?

Solution 2

Some alternatives to managing cascading drop down lists yourself are:

ASP.NET AJAX Control Toolkit: ASP.NET AJAX Cascading Drop Down

Cascading Drop Down Using ASP.NET and jQuery: Cascading Drop Down Using ASP.NET And jQuery

OTHER TIPS

Make sure you have ViewState enabled so it can populate the list before it "selects" the item. Also, make sure you don't repopulate in Page_Load and lose the selected value. eg. if (!IsPostback) { // Populate }

I was able to work around this problem by binding the dropdown in the code behind in page_init method with the proper !postback conditional instead of using the linqdatasource to bind it. I am still not sure what was causing it however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top