简体   繁体   English

asp:dropdownlist不保存用户选择的值c#asp.net aspx

[英]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: webapp aspx代码:

                <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: webapp aspx.cs代码:

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

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

webservice IOAuth2.cs code: webservice IOAuth2.cs代码:

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. 问题是下拉列表中的值是int ,但是您将SelectedValue设置为string。

Try: 尝试:

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

Hope it will help. 希望它会有所帮助。

You are mixing up the drodown's VALUE and TEXT properties. 你正在混淆drodown的VALUE和TEXT属性。 Your code here: 你的代码在这里:

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

Is trying to set the selected value to something like 'NotRequired'. 是否尝试将所选值设置为“NotRequired”。 That won't work, because your dropdown's VALUES are 0,1,2,etc. 这是行不通的,因为你的下拉值是0,1,2等等。

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. 最后,由于您使用的是EnumMember属性,我假设您正在序列化此枚举。 Relying on the integral values of a data-contract-serialized enum can have unexpected results. 依赖数据协定序列化枚举的整数值可能会产生意外结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#在asp.net中更改下拉列表选择的索引 - dropdownlist selected index changed in asp.net using C# ASP.NET和C#-在静态DropDownList中获取所选项目 - ASP.NET and C# - Get selected item in static DropDownList 如何更新下拉列表中的选定项asp.net c# - how to update a selected item in a dropdownlist asp.net c# 使用c#DropDownList项目的asp.net未选中 - asp.net using c# DropDownList item not selected GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值 - DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c# 无法在 C# .cs 文件中检索 ASP.NET webform 的选定下拉列表值 - Unable to retrieve selected dropdownlist value of ASP.NET webform in C# .cs file 3Layer C#asp.net:插入dropdownlist选中的文本和值 - 3Layer C# asp.net: insert dropdownlist selected text and value 在 C# 的 asp.net 下拉列表中的 select2 上设置多个选定的值 - set multiple selected value on select2 multiple in asp.net dropdownlist from C# C#ASP.Net-如何显示基于DropDownList中选定值的文本框 - C# ASP.Net - How to Display TextBox Based on Selected Value in DropDownList 按钮单击事件C#ASP.NET之后,DataGrid中的静态DropDownList返回错误的选定值 - Static DropDownList in DataGrid returns wrong selected value after button click event C# ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM