Friday, 3 October 2014

Validate at least one Checkbox is checked or not in GridView using JQuery

validate checkbox in gridview

Description:-



In this example we explain that how to check atleast one checkbox is checked or not in gridview checkbox in asp.net using javascript and jquery. Or how to validate to check at least one checkbox is checked in the Gridview in ASP.NET using JQuery.

This is common requirement in every webforms in application like in Gridview we provide facility to end user to delete multiple rows in gridview by selecting or checked checkbox in gridview. So there is a requirement when user click on delete button at that first we must have to validate or check that atleast one checkbox is checked in gridview or not. If user not checked any checkbox in gridview and directally click on Delete button then proper alert message will be display like "please select/check atleast one checkbox row that you want to delete".

So how to perform or validate checkbox is checked or not in gridview in asp.net using javascript/jquery.

Dynamically Bind Data in Ajax Accordion Panel Bind Data to Accordion from databse


Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user



 Default3.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to Create Watermark Text for TexBox in asp.net using jquery</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
    <script language="javascript">
        $(document).ready(function () {
            $('#btn_submit').click(function () {
                var chkboxrowcount = $("#<%=gv_employee.ClientID%> input[id*='chk_selectrow']:checkbox:checked").size();
                if (chkboxrowcount == 0) {
                    alert("please select at least one record");
                    return false;
                }
                return true;
            });
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gv_employee" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    Select CheckBox
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="chk_selectrow" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Emp_ID" HeaderText="Emp_ID" />
            <asp:BoundField DataField="Emp_name" HeaderText="Emp_name" />
            <asp:BoundField DataField="Address" HeaderText="Address" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="btn_submit" Text="Submit" runat="server" />
    </form>
</body>
</html>

 Default3.aspx.cs:-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Emp_ID");
            dt.Columns.Add("Emp_name");
            dt.Columns.Add("Address");

            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "kirit";
            dr[2] = "Ahmedabad";
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1[0] = 2;
            dr1[1] = "pintu";
            dr1[2] = "Rajkot";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr1[0] = 3;
            dr1[1] = "Rahul";
            dr1[2] = "surat";
            dt.Rows.Add(dr2);
            gv_employee.DataSource = dt;
            gv_employee.DataBind();
        }


    }
}

0 comments:

Post a Comment