ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.At least one CheckBox checked validation in each row of ASP.Net GridView on Button Click using C# and VB.Net<p>Hey <a class="username" rel="sathish75">sathish75</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;Gridview1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Morning&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkMrng&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Afternoon&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkNoon&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Night&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkNyt&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; Text=&#34;Save&#34; OnClick=&#34;Validate&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;CustomerId&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Mudassar&#34;); dt.Rows.Add(2, &#34;John&#34;); dt.Rows.Add(3, &#34;Robert&#34;); this.Gridview1.DataSource = dt; this.Gridview1.DataBind(); } } protected void Validate(object sender, EventArgs e) { foreach (GridViewRow row in Gridview1.Rows) { CheckBox chkMorning = row.FindControl(&#34;chkMrng&#34;) as CheckBox; CheckBox chkAfterNoon = row.FindControl(&#34;chkNoon&#34;) as CheckBox; CheckBox chkNyt = row.FindControl(&#34;chkNyt&#34;) as CheckBox; bool checkedChk = false; if (chkMorning.Checked) { checkedChk = true; } else if (chkAfterNoon.Checked) { checkedChk = true; } else if (chkNyt.Checked) { checkedChk = true; } if (!checkedChk) { ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;CropImage&#34;, &#34;alert(&#39;Atlease one checkbox should be selected in row &#34; + (row.RowIndex + 1) + &#34;&#39;);&#34;, true); break; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;CustomerId&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Mudassar&#34;) dt.Rows.Add(2, &#34;John&#34;) dt.Rows.Add(3, &#34;Robert&#34;) Me.Gridview1.DataSource = dt Me.Gridview1.DataBind() End If End Sub Protected Sub Validate(ByVal sender As Object, ByVal e As EventArgs) For Each row As GridViewRow In Gridview1.Rows Dim chkMorning As CheckBox = TryCast(row.FindControl(&#34;chkMrng&#34;), CheckBox) Dim chkAfterNoon As CheckBox = TryCast(row.FindControl(&#34;chkNoon&#34;), CheckBox) Dim chkNyt As CheckBox = TryCast(row.FindControl(&#34;chkNyt&#34;), CheckBox) Dim checkedChk As Boolean = False If chkMorning.Checked Then checkedChk = True ElseIf chkAfterNoon.Checked Then checkedChk = True ElseIf chkNyt.Checked Then checkedChk = True End If If Not checkedChk Then ScriptManager.RegisterStartupScript(Me, Me.[GetType](), &#34;CropImage&#34;, &#34;alert(&#39;Atlease one checkbox should be selected in row &#34; &amp; (row.RowIndex + 1) &amp; &#34;&#39;);&#34;, True) Exit For End If Next End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/BZm8OYj.gif" alt="" width="451" height="236" /></p>https://www.aspforums.net:443/Threads/585048/At-least-one-CheckBox-checked-validation-in-each-row-of-ASPNet-GridView-on-Button-Click-using-C-and-VBNet/https://www.aspforums.net:443/Threads/585048/At-least-one-CheckBox-checked-validation-in-each-row-of-ASPNet-GridView-on-Button-Click-using-C-and-VBNet/Tue, 04 Dec 2018 06:09:01 GMT