Giter Site home page Giter Site logo

devexpress-examples / asp-net-web-forms-grid-calculate-templated-column-values-on-the-client Goto Github PK

View Code? Open in Web Editor NEW
0.0 55.0 1.0 953 KB

Create an unbound column (Total) that changes its value based on templated column values.

License: Other

C# 21.38% ASP.NET 57.13% Visual Basic .NET 21.49%
dotnet aspxgridview asp-net templates unbound-column

asp-net-web-forms-grid-calculate-templated-column-values-on-the-client's Introduction

Grid View for ASP.NET Web Forms - How to calculate templated column values on the client

This example demonstrates how to create an unbound column (Total) that changes its value based on templated column values.

Templates Column Values

Overview

Follow the steps below to calculate templated column values on the client:

  1. Specify a data column's DataItemTemplate property, add a spin editor to the template, and bind the editor to the corresponding column values.

    <dx:GridViewDataTextColumn FieldName="UnitsInStock" VisibleIndex="5">
        <DataItemTemplate>
            <dx:ASPxSpinEdit ID="seUnitInStock" runat="server" Number="0" Value='<%# Bind("UnitsInStock") %>'
                OnInit="seQuantity_Init" />
        </DataItemTemplate>
    </dx:GridViewDataTextColumn>
  2. Create an unbound column to display the calculation results, specify the column's DataItemTemplate property, and add a text box editor to the template.

    <dx:GridViewDataTextColumn FieldName="Total" VisibleIndex="6" UnboundType="Decimal">
        <DataItemTemplate>
            <dx:ASPxTextBox ID="tbTotal" runat="server" ClientEnabled="False" OnInit="tbTotal_Init" />
        </DataItemTemplate>
        <!-- ... -->
    </dx:GridViewDataTextColumn>
  3. Handle the grid's server-side CustomUnboundColumnData event to calculate unbound column values.

    protected void grdProducts_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridViewColumnDataEventArgs e) {
        if(e.Column.FieldName == "Total") {
            decimal price = (decimal)e.GetListSourceFieldValue("UnitPrice");
            int quantity = Convert.ToInt32(e.GetListSourceFieldValue("UnitsInStock"));
            e.Value = price * quantity;
        }
    }
  4. Handle the spin editor's client-side NumberChanged event to calculate the resulting values of the unbound column. Use a hidden field control to store the calculated values.

    function OnCalculateTotal(visibleIndex, keyValue) {
        var controlCollection = ASPxClientControl.GetControlCollection();
        var unitPrice = controlCollection.GetByName("seClientPrice_" + visibleIndex).GetNumber();
        var unitsInStock = controlCollection.GetByName("seClientQuantity_" + visibleIndex).GetNumber();
        var total = unitPrice * unitsInStock;
        controlCollection.GetByName("tbClientTotal_" + visibleIndex).SetText(total.toFixed(2));
        hfChanges.Set("Row_" + visibleIndex.toString(), keyValue + "|" + unitPrice + "|" + unitsInStock);
    }
    <dx:ASPxHiddenField ID="hf" runat="server" ClientInstanceName="hfChanges" />
    protected void seQuantity_Init(object sender, EventArgs e) {
        ASPxSpinEdit spinEdit = (ASPxSpinEdit)sender;
        GridViewDataItemTemplateContainer container = spinEdit.NamingContainer as GridViewDataItemTemplateContainer;
        spinEdit.ClientInstanceName = String.Format("seClientQuantity_{0}", container.VisibleIndex);
        spinEdit.ClientSideEvents.NumberChanged = String.Format("function(s, e) {{ OnCalculateTotal({0},{1}); }}", container.VisibleIndex, container.KeyValue);
    }
  5. To update the data source, send a postback to the server on a button click.

Files to Review

Documentation

More Examples

asp-net-web-forms-grid-calculate-templated-column-values-on-the-client's People

Contributors

devexpressexamplebot avatar elenapeskova avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

elenapeskova

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.