views:

225

answers:

2

I have an ASP ListView, and have a very simple requirement to display numbers as formatted w/ a comma (12,123), while they need to bind to the database without formatting (12123). I am using a standard setup - ListView with a datasource attached, using Bind().

I converted from some older code, so I'm not using ASP.NET controls, just form inputs...but I don't think it matters for this:

<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>

In the example above, NUMSTR is a number, but stored as a string in a SqlServer 2008 database. I'm also using the ItemTemplate as read and edit templates, to save on duplicate HTML. In the example, I only get the unformatted number. If I convert the field to an integer (via the SELECT) and use a format string like Bind("NUMSTR", "{0:###,###}"), it writes the formatted number to the database, and then fails when it tries to read it again (can't convert with the comma in there).

Is there any elegant/simple solution to this? It's so easy to get the two-way binding going, and I would think there has to be a way to easily format things as well...

Oh, and I'm trying to avoid the standard ItemTemplate and EditItemTemplate approach, just for sheer amount of markup required for that.

Thanks!

A: 

Cannot you use <%# Bind("expression"[, "format"]) %> style? So in your case, devise a format string (I think it should be "#,###") and ASP.NET would be able to format the string both on input and output.

Axarydax
Using that method causes the formatted value to write to the database as well, which is not the desired functionality. I need it to display as formated, write as unformatted. Thanks for the input, though :)
chucknelson
A: 

As seen in my comment above, I ended up just stripping commas from the NewValues collection in the ListView ItemUpdating event, and adding in commas on the ItemDataBound event.

If there are other ways to do this in a clean way, I'm interested!

Code in VB.NET, comment syntax made to be compatible with stackoverflow's highlighting:

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
chucknelson