Click here to Skip to main content
16,018,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code there is a textbox and another label commission,the commission will be directly bind from the database and the textbox is to assign a new commisiion ,now i have to check that textbox value which has been entered dynamically by the user,if the entered value is greater than the value (i.e in tha label"commission") it should promt an error msg that" please enter the value less than the commission

and I am using javascript also to retrieve the textbox values ..but the textbox value is returning as null

What I have tried:

<asp:GridView ID="gvOprList" runat="server" AutoGenerateColumns="False" CssClass="table table-hover table-bordered" OnRowDataBound="gvOprList_RowDataBound">



protected void gvOprList_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gr in gvOprList.Rows)
{
TextBox txtCommission = gr.FindControl("txtNewCommission") as TextBox;
Label lblCommission = gr.FindControl("lblOprCommission") as Label;
txtCommission.Attributes.Add("onkeyPress", "javascript:Calculation(" + e.Row.RowIndex + ",' " + (txtCommission.ClientID) + " ',' " + (lblCommission.Text) + " ')");
}
}
Posted
Updated 20-Sep-16 6:33am
Comments
Karthik_Mahalingam 20-Sep-16 9:47am    
use onkeyup event

1 solution

I would suggest you to handle it at client-side instead. You could easily do that with jQuery. Here's a quick example:

HTML:
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="txtCommission" placeholder="Enter value" runat="server" CssClass="txtField" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCommission" runat="server" CssClass="lblField" Text='<%# Bind("Commission") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


jQuery Reference:
<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>


JavaScript:
$(function () {
            $(".txtField").keyup(checkValue);

            function checkValue() {
                // loop through the rows
                $(".txtField").each(function () {
                    // get the values from this row:
                    var $val1 = parseInt($(this).val()) || 0;
                    var $val2 = parseInt($(this).parent().parent().find(".lblField").text()) || 0;

                    if ($val1 > $val2) {
                        alert("Please enter a value less than the commission");
                        $(this).focus();
                        return false;
                    }
                    
                });
            }
        });


CODE BEHIND:

using System;
using System.Collections.Generic;

namespace WebFormsDemo
{
    public class Item
    {
        public string Name { get; set; }
        public int Commission { get; set; }
    }
    public partial class JavaScript : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Item> item = new List<Item>();
                item.Add(new Item { Name = "Item1", Commission = 100 });
                item.Add(new Item { Name = "Item2", Commission = 200 });
                item.Add(new Item { Name = "Item3", Commission = 300 });
                item.Add(new Item { Name = "Item4", Commission = 400 });
                item.Add(new Item { Name = "Item5", Commission = 500 });

                gv1.DataSource = item;
                gv1.DataBind();

            }
        }
    }
}


That should work based on your requirements.
 
Share this answer
 
v7
Comments
Member 12749751 21-Sep-16 3:52am    
sir...this javascript code is giving me error on first line "$(function () {"

please advice any other option
F-ES Sitecore 21-Sep-16 4:06am    
You need to add a script reference to jQuery.
Vincent Maverick Durano 21-Sep-16 9:22am    
I wanted to include the jQuery reference from the code, but when I do, it broke the code formatting. You need to include the reference to jQuery as suggested.

I've updated the solution and included the jQuery reference. You need to add it before your JavaScript.
Member 12749751 22-Sep-16 4:27am    
Sir..Its Done..thank you so much
Vincent Maverick Durano 22-Sep-16 6:06am    
great! One more thing though, don't forget to close this thread by marking the post that helps as answer for the sake of future readers. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900