[RESOLVED]Validate enter value inside grid

In my gridview have to check enter value for template field is greater than or equal in my data keyname value in my grid view. If its greater than i want to show message (label control). Now i used compare validation control for it . need javascript

Thnks

In  following code, I’ve used data-item attribute to have column value(to be compared) and span for displaying message:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataKeyNames="Items">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txt1" CssClass="txt1" data-item='<%# Eval("Items")%>'>
                    </asp:TextBox>
                    <span style="display:none;color:red">Greater value</span>
                 </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

<script type="text/javascript">
        $('.txt1').blur(function () {
            if (parseInt($(this).val()) > parseInt($(this).data('item'))) {
                $(this).next().show();
                $(this).focus();
                return false;
            }
            else {
                $(this).next().hide();
            }
        })
    </script>


On CS side:

        Person[] persons = {
                               new Person { FirstName="Damon", Items=20 },
new Person { FirstName="Niki", Items=25 },
new Person { FirstName="Ayrton", Items=20 },
new Person { FirstName="Graham", Items=52 }
};

        GridView1.DataSource = persons;
        GridView1.DataBind();

Hope, It helps.

Dear joy.. 

This one isnt work.

Tested again …It’s working fine…

Make sure you are using jquery 1.5 or above…

what error are you getting?

i use vs 3.5 . I didnt get any error or anything. 

Check following rendered html demo:

http://jsfiddle.net/WFHZm/

Hi,

could u post a sample code which can simulate ur problem
with minimal coding? so that anyone can help in that.

Here 1st and 2nd coloumn binding from dataset. and datakey also comming from dataset. i want to compare my enter value in 4th coloumn  with datakey

<script>
function DoSum(textBox) {
var row = textBox.parentNode.parentNode; 
var value3 = parseFloat(row.childNodes[2].childNodes[0].value);
var value4 = parseFloat(row.childNodes[3].childNodes[0].value);
row.childNodes[4].childNodes[0].value = (isNaN(value3) ? 0 : value3) + (isNaN(value4) ? 0 : value4);
}
</script>

<asp:GridView ID="grvReturnNote" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" 
BorderStyle="Solid" BorderWidth="1px" CellPadding="2" CssClass="Gridtablestyle" 
EmptyDataRowStyle-CssClass="Labelstyle" EmptyDataText="No records to display" 
Height="16px" ShowFooter="True" TabIndex="13" Width="903px" 
DataKeyNames="SaleQty" 
onrowdatabound="grvReturnNote_RowDataBound">
<RowStyle ForeColor="#000066" />
<EmptyDataRowStyle BorderStyle="None" BorderWidth="0px" 
HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Description2" HeaderStyle-CssClass="GridPadding" 
HeaderText="Item">
<HeaderStyle CssClass="GridPadding" />
</asp:BoundField>
<asp:TemplateField HeaderText="Total Returns from Sales Rep">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text=’<%# Bind("DamageQty") %>’></asp:TextBox>
</EditItemTemplate>
<ItemTemplate> 
<asp:TextBox ID="txtDmgqty" runat="server" BorderStyle="None" Text=’<%# Bind("DamageQty") %>’></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Distributor sendfrom SalesRep">
<ItemTemplate>
<asp:TextBox ID="txtDeliDate" runat="server" Height="21px" 
style="text-align: right" TabIndex="6" 
Width="86px" CssClass="Textboxstyle" 
onchange="DoSum(this)" ValidationGroup="msb"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" 
ControlToValidate="txtDeliDate" ErrorMessage="*" 
Operator="LessThanEqual" ControlToCompare="txtDmgqty" 
style="text-align: left" Type="Double" ValidationGroup="msb"></asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distributor Return QTY">
<ItemTemplate>
<asp:TextBox ID="txtDRQTY" runat="server" CssClass="Textboxstyle" Height="21px" 
Width="100px" onchange="DoSum(this)"></asp:TextBox>
<span style="display:none;color:red">Greater value</span>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total Return QTY">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>

<asp:TextBox ID="txtTotreturns" runat="server" BorderStyle="None" 
ReadOnly="True" Width="100px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Distributor Return Reason">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="Textboxstyle" 
Width="86px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>

protected void grvReturnNote_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Session["dtReason"] != null)
{
DataTable dtReason = (DataTable)Session["dtReason"];
DropDownList ddlReturns = (DropDownList)e.Row.FindControl("DropDownList1");
ddlReturns.DataSource = dtReason;
ddlReturns.DataTextField = "Description";
ddlReturns.DataValueField = "ReturnType";
ddlReturns.DataBind();
}

}
}

Exactly i need like your demo. 

Here i havent binding to by template . 

ItemTemplate>
                    <asp:TextBox runat="server" ID="txt1" CssClass="txt1" data-item='<%# Eval("Items")%>'>
                    </asp:TextBox>
                    <span style="display:none;color:red">Greater value</span>
                 </ItemTemplate>

so i cant go ahead .
Thanks joy

As per your code:

<asp:TemplateField HeaderText="Distributor Return QTY">
<ItemTemplate>
<asp:TextBox ID="txtDRQTY" runat="server" CssClass="Textboxstyle txt1" Height="21px"  data-item='<%# Eval("SaleQty")%>'
Width="100px" onchange="DoSum(this)"></asp:TextBox>
<span style="display:none;color:red">Greater value</span>
</ItemTemplate>
</asp:TemplateField>

<script type="text/javascript">
        $('.txt1').blur(function () {
            if (parseInt($(this).val()) > parseInt($(this).data('item'))) {
                $(this).next().show();
                $(this).focus();
                return false;
            }
            else {
                $(this).next().hide();
            }
        })
    </script>

Leave a Reply