Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:33:36+00:00 2026-06-06T13:33:36+00:00

I have a ListView that displays a shopping cart. It was working fine until

  • 0

I have a ListView that displays a shopping cart. It was working fine until I added the String.Format to display the LineTotal decimal as a currency string. It was working when I just had an Eval on LineTotal.

The problem happens when I add the String.Format–it messes up the codebehind. Error: Input string was not in a correct format.

In the C#, I take the value of the text label and use that throughout the codebehind to do things like add up the total value (sum var) of products.

I want the PriceLabel price to display as currency, but ALSO to be able to use the value of the label in my Listview databound function as well so that I can update the sum var.

Abbreviated ItemTemplate:

<ItemTemplate>               
<asp:Label ID="PriceLabel" runat="server" Text='<%# String.Format("{0:C}", Eval("LineTotal"))%>' ></asp:Label>
</ItemTemplate>

Codebehind:

    protected void ProductListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
        if (e.Item.ItemType == ListViewItemType.DataItem)
            {
            //update subtotal by finding the label string value
            Label lbl = (Label)e.Item.FindControl("PriceLabel") as Label;

            decimal sublinetotal = Convert.ToDecimal(lbl.Text);// <---- Input error here

            //update the sum var to show the total price on the page
            sum += sublinetotal;

            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:33:40+00:00Added an answer on June 6, 2026 at 1:33 pm

    I think your approach is not really good in this case.

    Firstly, the error you got is because lbl.Text is probably empty since the binding is being made (see, the value in the ASPX file will be set AFTER ItemDataBound event). So, you’d be better to set read the DataItem directly:

    var row = e.Item.DataItem as System.Data.DataRowView;
    if (row != null) {
      sum += row["LineTotal"];
    }
    

    See: http://msdn.microsoft.com/en-us/library/bb299031.aspx for more information.

    However, a more robust approach would be to calculate this BEFORE any data binding. So, this would be reusable, and the view wouldn’t have to calculate all this:

    public class InvoiceLine {
      public Decimal Line { get; set; }
    }
    
    public class Invoice {
      public IList<InvoiceLine> Lines { get; set; }
      public Decimal Total {
        get {
          return Lines.Sum(l => l.Line);
        }
      }
    }
    
    protected void Page_Load(...) {
      var invoice = SomeInvoiceService.GetInvoice(id);
      ProductListView.DataSource = invoice.Lines;
      ProductListView.DataBind();
    
      TotalLabel.Text = String.Format("{0:C}", invoice.Total);
    }
    

    ASPX FILE:

    <asp:ListView ID="ProductListView" runat="server">
      <ItemTemplate>
        <%# String.Format("{0:C}", Eval("Line")); %>
      </ItemTemplate>
    </asp:ListView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview that displays a list of profiles added by a user.
I have a ListView that displays a link button. I need the link button
I have ListView that uses a GridView to display several columns of data. Two
I have a ListView where I would like to display things horizontally. That works
In one of my Activities I have a ListView that displays a list of
I have a ListView-like control that displays a list of items of various heights.
I have a ListView that displays error to the user. Part of it also
I have a custom listview that displays approx. 114 items, and in it I
I have a Listview that pulls and displays some data. Field 1 is the
I have a ListView that displays a few TextBoxes . For each TextBox ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.