"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Friday, February 1, 2013

addition operation in Grid inside the text box using javascript

Having addition operation on gridview in JavaScript.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
        $(document).ready(function () {
            //  alert('hai');
            $(':text').blur(function () {
                sum = +0;
                var attrValue = $(this).attr('attr-id');
                $(':text').each(function () {
                    if ($(this).attr('attr-id') == attrValue) {
                        var v = +$(this).val();
                        sum = (sum + v);
                    }
                });
                alert(sum);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txt01" Text='<%# Eval("val01") %>' attr-id='<%# Eval("ID") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txt02" Text='<%# Eval("val02") %>' attr-id='<%# Eval("ID") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txt03" Text='<%# Eval("val03") %>' attr-id='<%# Eval("ID") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable
        {
            Columns ={
                {"ID",typeof(int)},
                {"val01",typeof(int)},
                {"val02",typeof(int)},
                {"val03",typeof(int)}
                }
        };
        dt.Rows.Add(1, 2, 3, 4);
        dt.Rows.Add(2, 2, 3, 4);
        dt.Rows.Add(3, 2, 3, 4);
        dt.Rows.Add(4, 2, 3, 4);
        grd.DataSource = dt;
        grd.DataBind();
    }





If u had any trouble just ask, Happy to help u :)
Stay Tune... Have a nice day... 'N happy Coding :)

No comments: