简体   繁体   中英

DropDownList TextValue is 1st in List

I'm a newbie when it comes to asp.net, so grateful for any help.

I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.

The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:

DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;

And here's the code form the aspx page

<asp:TemplateField HeaderText="Preferred Location"         
    SortExpression="PreferredLocation">        
    <ItemTemplate>        
        <asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />        
    </ItemTemplate>    
    <EditItemTemplate>    
        <asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />    
    </EditItemTemplate>    
    <InsertItemTemplate>    
        <asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />    
    </InsertItemTemplate>
</asp:TemplateField>

<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    SelectCommand="SELECT * FROM StaticData" />

You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM