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

标签 c# asp.net enums

几个小时以来,我一直在努力让它发挥作用。我认为问题在于使用枚举,但我的老板坚持要我们使用它。

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代码:

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

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

网络服务IOAuth2.cs代码:

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

最佳答案

问题是下拉列表中的值是 int,但您将 SelectedValue 设置为字符串。

尝试:

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

希望对您有所帮助。

关于c# - asp :dropdownlist doesn't save users selected value c# asp. 净aspx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074287/

相关文章:

c# - 从强类型列表填充下拉列表

asp.net - 你能确定你的网络表单页面遵循的路线名称吗?

Java:为什么不能编译?

parameters - 如何更改枚举中的参数值(Swift)

c# - 使用 XmlTextWriter 设置多个命名空间

c# - Unity c# - NullReferenceException : Object reference not set to an instance of an object

c# - #eval GridView 中的电话号码

c# - ASP.net 的查询字符串 ID

asp.net - NHibernate log4net 性能问题 - 如何禁用?

编译的枚举常量