Asp.net ASP ListView-Eval()作为格式化的数字,Bind()作为未格式化的数字?

Asp.net ASP ListView-Eval()作为格式化的数字,Bind()作为未格式化的数字?,asp.net,data-binding,listview,Asp.net,Data Binding,Listview,我有一个ASP ListView,并且有一个非常简单的要求,即以带逗号12123的格式显示数字,而它们需要绑定到数据库而不格式化12123。我使用的是标准的setup-ListView,附带了一个数据源,使用Bind 我从一些旧代码转换而来,所以我不使用ASP.NET控件,只使用表单输入…但我认为这与此无关: <asp:SqlDataSource ID="MySqlDataSource" runat="server" ConnectionString='<%$ Connecti

我有一个ASP ListView,并且有一个非常简单的要求,即以带逗号12123的格式显示数字,而它们需要绑定到数据库而不格式化12123。我使用的是标准的setup-ListView,附带了一个数据源,使用Bind

我从一些旧代码转换而来,所以我不使用ASP.NET控件,只使用表单输入…但我认为这与此无关:

<asp:SqlDataSource ID="MySqlDataSource" runat="server" 
  ConnectionString='<%$ ConnectionStrings:ConnectionString1 %>' 
  SelectCommand="SELECT NUMSTR FROM MY_TABLE WHERE ID = @ID" 
  UpdateCommand= "UPDATE MY_TABLE SET NUMSTR = @NUMSTR WHERE ID = @ID">
</asp:SqlDataSource>

<asp:ListView ID="MyListView" runat="server" DataSourceID="MySqlDataSource">
  <LayoutTemplate>
    <div id="itemplaceholder" runat="server"></div>
  </LayoutTemplate>
  <ItemTemplate>
    <input type="text" name="NUMSTR" ID="NUMSTR" 
      runat="server" value='<%#Bind("NUMSTR")%>' />
    <asp:Button ID="UpdateButton" runat="server" Text="Update" Commandname="Update" />  
  </ItemTemplate>
</asp:ListView>
在上面的示例中,NUMSTR是一个数字,但作为字符串存储在SQLServer2008数据库中。我还将ItemTemplate用作读取和编辑模板,以保存重复的HTML。在这个例子中,我只得到未格式化的数字。如果我通过SELECT和use格式字符串(如BindNUMSTR,{0:,})将字段转换为整数,它会将格式化后的数字写入数据库,然后在尝试再次读取时失败。无法使用逗号进行转换

有什么优雅/简单的解决方案吗?双向绑定非常容易,而且我认为必须有一种方法可以轻松地格式化内容

哦,我正试图避免使用标准的ItemTemplate和EditItemTemplate方法,这仅仅是因为需要大量的标记

谢谢

您不能使用样式吗?
因此,在您的情况下,设计一个我认为应该的格式字符串,ASP.NET将能够在输入和输出上格式化字符串。

正如我在上面的评论中所看到的,我在ListView ItemUpdate事件中从NewValues集合中去掉逗号,并在ItemDataBound事件中添加逗号

如果有其他方法可以做到这一点,在一个干净的方式,我感兴趣

VB.NET中的代码,注释语法与stackoverflow的突出显示兼容:

Protected Sub myListView_OnItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles myListView.ItemDataBound
    Dim intNumber As Integer
    For Each c As Control In e.Item.Controls
        If TypeOf c Is TextBox Then /*ASP textbox controls*/
            Dim numberText As TextBox
            numberText = DirectCast(c, TextBox)
            If Integer.TryParse(numberText.Text, intNumber) Then /*If the text can be parsed, format it*/
                numberText.Text = String.Format("{0:###,##0}", intNumber)
        End If
    Next
End Sub

Protected Sub myListView_OnItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles myListView.ItemUpdating
    Dim cleanNumber As String
    Dim intNumber As Integer
    For Each key As String In e.NewValues.Keys
        cleanNumber = e.NewValues(key).ToString().Replace(",", Nothing) /*Remove all commas*/
        If Integer.TryParse(cleanNumber, intNumber) Then /*If the text can be parsed, format it*/
            e.NewValues(key) = intNumber.ToString()
        End If
    Next
End Sub

最后,我在ListView ItemUpdate事件中从NewValues集合中去掉逗号,并在ItemDataBound事件中添加逗号。还有其他干净的方法吗?使用该方法会导致格式化值也写入数据库,这不是所需的功能。我需要它显示为格式化,写入为未格式化。不过,感谢您的投入: