DoImagine
DoImagine

Reputation: 47

Dropdownlist looses value during autopostback

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>

Upvotes: 0

Views: 1225

Answers (3)

DoImagine
DoImagine

Reputation: 47

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.

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

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

Upvotes: 0

Ashish Mulaye
Ashish Mulaye

Reputation: 256

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 }

Upvotes: 2

Related Questions