将DropDownList绑定为十进制数

时间:2013-12-14 23:16:33

标签: c# asp.net data-binding

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

<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中选择“Half Price”选项(“0.5”值)并按更新按钮时,我将收到此错误:

Input string was not in a correct format

但“免费”选项很好。

当我在数据库中直接设置0.5值时,我会在editig记录时出现此错误:

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}") %>'

我真的很困惑!

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

最后我找到了原因!

我的CurrentCulture'fa-IR'。在此文化中,DecimalSeperator为/而非.

添加此代码后,修复了问题:

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