[英]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.