【问题标题】:ASP ListView - Eval() as formatted number, Bind() as unformatted?ASP ListView - Eval()作为格式化数字,Bind()作为未格式化?
【发布时间】:2010-03-24 18:45:23
【问题描述】:

我有一个 ASP ListView,并且有一个非常简单的要求,即以带逗号的格式显示数字 (12,123),而它们需要绑定到数据库而不进行格式化 (12123)。我正在使用标准设置 - 使用 Bind() 附加数据源的 ListView。

我从一些旧代码转换而来,所以我没有使用 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 是一个数字,但在 SqlServer 2008 数据库中存储为字符串。我还使用ItemTemplate 作为阅读和编辑模板,以保存重复的 HTML。在示例中,我只得到未格式化的数字。如果我将该字段转换为整数(通过SELECT)并使用像Bind("NUMSTR", "{0:###,###}") 这样的格式字符串,它将格式化的数字写入数据库,然后在尝试再次读取它时失败(无法转换为逗号)。

对此有什么优雅/简单的解决方案吗?进行双向绑定非常容易,我认为必须有一种方法可以轻松格式化事物......

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

谢谢!

【问题讨论】:

  • 我最终只是从 ListView ItemUpdating 事件中的 NewValues 集合中删除了逗号,并在 ItemDataBound 事件中添加了逗号。还有其他方法可以干净地做到这一点吗?

标签: asp.net data-binding listview


【解决方案1】:

正如我在上面的评论中所看到的,我最终只是从 ListView ItemUpdating 事件中的 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

【讨论】:

    【解决方案2】:

    你不能使用 样式吗? 因此,在您的情况下,设计一个格式字符串(我认为它应该是“#,###”)并且 ASP.NET 将能够在输入和输出时格式化该字符串。

    【讨论】:

    • 使用该方法会导致格式化值也写入数据库,这不是所需的功能。我需要它显示为格式化,写为未格式化。不过,感谢您的输入:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多