Saturday, July 2, 2011

Row level client side operation on gridview

Hi All,

Last weekend while helping one of my friends on his project, I needed to perform row level client side operation on gridview. Firstly I googled for finding something similar to “formulae in excel” for asp gridview. When I failed to find something good I decided to go with javascript to perform client side operation on individual gridview rows.

My scenario was of invoice sheet generation, where need was of addition of rows(one for each product selected) dynamically and then perform some calculation like displaying row wise total. On click of Add button new record for item selected in drop down is added(this is done from server side code). Here user can enter values in any of the textboxes. And system has to update values in Total Excessible Value column text box(see image below) accordingly.

For this I wrote two plain javascript functions and called them on onchange event of textbox. Below are grid view and jquery code that I used.

(Click to enlarge)

GridView that I used was below:

<asp:GridView ID="gdvInvoiceItems" runat="server" AutoGenerateColumns="False" ShowHeader="true"
            No items added yet.
            <asp:TemplateField HeaderText="S No.">
                    <%# Container.DataItemIndex + 1 %>
                    <asp:HiddenField ID="hdnItemId" runat="server" Value='<%# Eval("Id") %>' />
            <asp:TemplateField HeaderText="Description of Goods" ItemStyle-CssClass="description">
                    <asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Description") %>'
            <asp:TemplateField HeaderText="Quantity(Net)" ItemStyle-CssClass="quantity">
                    <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("Quantity") %>' onchange="javascript:Multiply(this, this.value)">asp:TextBox>
            <asp:TemplateField HeaderText="Value Per Unit" ItemStyle-CssClass="value">
                    <asp:TextBox ID="txtValue" runat="server" Text='<%# Eval("PerUnitValue") %>' onchange="javascript:Multiply(this, this.value)">asp:TextBox>
            <asp:TemplateField HeaderText="Total Exesible Value" ItemStyle-CssClass="exesiblevalue">
                    <asp:TextBox ID="txtTotalExeValue" runat="server" Text='<%# Eval("Total") %>' CssClass="TotalExeValue">asp:TextBox>
                    <asp:LinkButton ID="lnkBtnRemove" runat="server" Text="Remove" ItemStyle-CssClass="remove">

JavaScript code used:

<script type="text/javascript">
      //This method is called when quantity/cost textbox looses focus with some change in content
      function Multiply(element, val)
          var otherElementName = '';
          var finalElementName = '';
          //id of calling element i.e, quantity/cost textbox
          var elementName =;
        //get second element, i.e., get quantity if change is in cost and vice-versa
          if(endsWith(elementName, "txtQuantity"))
              otherElementName = elementName.replace("txtQuantity", "txtValue");
          else if(endsWith(elementName, "txtValue"))
              otherElementName = elementName.replace("txtValue", "txtQuantity");
          var otherElement = document.getElementById(otherElementName);
          //get textbox where final value is to be displayed
        finalElementName = elementName.replace("txtValue", "txtTotalExeValue")         
          var finalElement = document.getElementById(finalElementName);
          finalElement.value = otherElement.value * val;
      //checks if given string ends with given suffix
      function endsWith(str, suffix) {
        return str.indexOf(suffix, str.length - suffix.length) !== -1;


Highlighted lines onchange="javascript:Multiply(this, this.value)" are the one that are doing the magic. This onchange event doesn't comes in intellisense but it works.

No comments:

Post a Comment

About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!