Friday, September 21, 2012

ASP.NET : Validate In place edit GridView using jQuery.

ASP.NET : Validate In place edit GridView using jQuery.

This very easy demo will show you how to validate TextBox in GridView by using jQuery.
Demo Code show below.

The Result

 CustomerIDCustomerName
Update Cancel 0 *
Edit 1 Dow jones
Edit 2 Million Dollar
Edit 3 Bank of America

1 : Add modify Template in GridView set OnClientClick="return validateGridView();"

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="lbtEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="lbtUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" OnClientClick="return validateGridView();"></asp:LinkButton>
                 
                <asp:LinkButton ID="lbtCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>
    

2 : Add Alert Label near Require Text.

        <asp:Label ID="lblReqCustomerName" runat="server" ForeColor="Red" Text="*" style="display:none;" CssClass="lblReqCustomerName"></asp:Label>
    

3 : Add Javascript Validate Function.

    function validateGridView() {
        if ($(".txtCustomerName").val() == "") {
            $(".lblReqCustomerName").show();
            $(".txtCustomerName").focus();
            return false;
        }
        else {
            $(".lblReqCustomerName").hide();
            return true;
        }
    }
    

DownloadDemo Here

Don't hesitate to contact me zomdev.

No comments:

Post a Comment