Displaying a running total is always good for the audience. You can display a running total by using a server side button click or by using a simple Javascript code. In this article I will demonstrate that how you can display the running total using the Javascript code. The running total will be displayed inside the GridView control.
Introduction: Displaying a running total is always good for the audience. You can display a running total by using a server side button click or by using a simple Javascript code. In this article I will demonstrate that how you can display the running total using the Javascript code. The running total will be displayed inside the GridView control. Creating the GridView: The GridView consists of two columns namely “Product Name” and the Price. The product name is displayed from the database table and the price is inserted by the user. Once, the price is inserted the total is displayed in the footer of the GridView control. Take a look at the code below:
Populating the GridView Control: I have created a custom table “Sales” which is populated with dummy data. Check out the code below which uses the DataSet to populate the GridView control.
Since, we will be keeping the running total and we don’t want to click any button to update the total so, we will use JavaScript to do the magic. The onChange event of the TextBox is fired whenever the value inside the TextBox is changed. Take a look at the code below which attaches the onChange event to the TextBox.
Take a look at the screen shot below which shows you the running total: In this article I showed that how you can use the simple JavaScript function to perform a running total. Using JavaScript in this scenario is a good idea since you don’t want to refresh the page whenever the user changes the value in the TextBox. I hope you liked the article, happy coding Update: One of the readers pointed out a bug in the sample code which is that the amount is not added when the user edit the TextBoxes. I have fixed the bug and uploaded the latest sample code. You can download the sample code from the link below. PS: I have also blogged about this bug which can be viewed at the following url http://geekswithblogs.net/azamsharp/archive/2006/07/05/84133.aspx |