将DropDownList绑定到十进制数字

本文关键字:十进制数字 绑定 DropDownList | 更新日期: 2024-09-26 00:32:47

我在FormView的EditItemTemplate中有一个DropDownList,它绑定到具有real数据类型的数据字段。

<asp:FormView ...>
    ...
    <EditItemTemplate>
        ...
        <asp:DropDownList ID="ddlKidPriceScale" runat="server" SelectedValue='<%# Bind("KidPriceScale") %>'>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem Value="0">Free</asp:ListItem>
            <asp:ListItem Value="0.5">Half Price</asp:ListItem>
        </asp:DropDownList>
        ....
    </EditItemTemplate>
    ....
</asp:FormView>

当我从DropDownlist中选择"半价"选项("0.5"值)并按下更新按钮时,我会得到这个错误:

Input string was not in a correct format

但"免费"选项是好的。

当我直接在数据库中设置0.5值时,我在编辑记录时会得到这个错误:

ddlKidPriceScale' has a SelectedValue which is invalid because it does not exist in the list of items

我也在绑定上下文中尝试过这些:

SelectedValue='<%# Bind("KidPriceScale2","{0:g}") %>'

SelectedValue='<%# Bind("KidPriceScale2","{0:f}") %>'

SelectedValue='<%# Bind("KidPriceScale2","{0:n}") %>'

我真的很困惑!

谢谢你的帮助!

将DropDownList绑定到十进制数字

终于找到了原因!

我的CurrentCulture'fa-IR'。在此区域性中,DecimalSeperator是/而不是.

添加此代码后,问题解决:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
    }