简体   繁体   中英

asp:dropdownlist doesn't save users selected value c# asp.net aspx

I've been trying to get this to work for hours. I think the problem is using enum but my boss insists we use it.

webapp aspx code:

                <label for="footerPlaceHolder_twoFactorAuthentication" class="sr-only"><asp:Localize runat="server" Text="<%$ Resources:UserMessages, TwoFactorSelect %>"></asp:Localize></label>
                <asp:DropDownList runat="server" ID="twoFactorAuthenticationDropDownList" CssClass="selectpicker">
                    <asp:ListItem Value="0" Text = "<%$ Resources:UserMessages, TwoFactorSelect %>"></asp:ListItem>
                    <asp:ListItem Value="3" Text = "<%$ Resources:UserMessages, TwoFactorRequire %>" ></asp:ListItem>
                    <asp:ListItem Value="2" Text = "<%$ Resources:UserMessages, TwoFactorRecommend %>" ></asp:ListItem>
                    <asp:ListItem Value="1" Text = "<%$ Resources:UserMessages, TwoFactorNotRequired %>" ></asp:ListItem>
                     </asp:DropDownList>
                <p><asp:RequiredFieldValidator runat="server" ControlToValidate="twoFactorAuthenticationDropDownList" CssClass="field-validation-error"  InitialValue="0" EnableClientScript="true" Display="Dynamic"></asp:RequiredFieldValidator></p>

webapp aspx.cs code:

var data = Utility.OAWebServiceClient.GetFullSiteData(rowToEdit);

            twoFactorAuthenticationDropDownList.SelectedValue = data.TwoFactorOption.ToString();

webservice IOAuth2.cs code:

public enum WebSite2FactorOptionEnum
{
    [EnumMember]
    NotSelected,
    [EnumMember]
    NotRequired,
    [EnumMember]
    Recommended,
    [EnumMember]
    Required
}

The problem is that the values in your drop down are int s, but you set SelectedValue to string.

Try:

twoFactorAuthenticationDropDownList.SelectedValue = ((int)data.TwoFactorOption).ToString();

Hope it will help.

You are mixing up the drodown's VALUE and TEXT properties. Your code here:

twoFactorAuthenticationDropDownList.SelectedValue = data.TwoFactorOption.ToString();

Is trying to set the selected value to something like 'NotRequired'. That won't work, because your dropdown's VALUES are 0,1,2,etc.

Something along these lines would work better:

twoFactorAuthenticationDropDownList.SelectedValue = ((int)data.TwoFactorOption).ToString();

Also, you should explicitly define the numeric values for your enum members, or else you are risking unintentional consequences if you change the order of items in your enum.

public enum WebSite2FactorOptionEnum
{
    [EnumMember]
    NotSelected = 0,
    [EnumMember]
    NotRequired = 1,
    [EnumMember]
    Recommended = 2,
    [EnumMember]
    Required = 3
}

Finally, since you are using the EnumMember attribute, I assume that you are serializing this enum. Relying on the integral values of a data-contract-serialized enum can have unexpected results.

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