Home > ASP.Net, Javascript, Jquery > GridView CheckBox Validation using JavaScript jQuery and ASP.Net CustomValidator

GridView CheckBox Validation using JavaScript jQuery and ASP.Net CustomValidator


Hi all,

try this example to Validate ChekBox inside Gridview using Javascript JQuery with Custom Validator Control

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type = "text/javascript">
function ValidateCheckBoxes(sender, args) {
args.IsValid = $("[id$=GridView1] input:checked").length > 1;
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Item" />
<asp:BoundField DataField="Code" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please check atleast one checkbox" ClientValidationFunction = "ValidateCheckBoxes"></asp:CustomValidator>

In Code behind :

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Code") });
dt.Rows.Add("Shirt", 199);
dt.Rows.Add("Football", 020);
dt.Rows.Add("Shirt", 566);
dt.Rows.Add("Disc", 099);
dt.Rows.Add("Watch", 54);
dt.Rows.Add("Clock", 890);
GridView1.DataSource = dt;
GridView1.DataBind();
}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net, Javascript, Jquery
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: