At least one CheckBox checked validation in each row of ASP.Net GridView on Button Click using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

I have 3 checkboxes Morning afternoon and night in dynamic gridview as i mentioned below code, i need to assign a validation least one checkbox should selected  .

same method as in this link. 

Maximum Row Validation when adding dynamic Rows to GridView on Button Click in ASP.Net

 

                                    <asp:TemplateField HeaderText="Morning">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkMrng" runat="server" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Afternoon">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkNoon" runat="server" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Night">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkNyt" runat="server" />
                                        </ItemTemplate>
                                    </asp:TemplateField>

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hey sathish75,

Please refer below sample.

HTML

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Morning">
            <ItemTemplate>
                <asp:CheckBox ID="chkMrng" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Afternoon">
            <ItemTemplate>
                <asp:CheckBox ID="chkNoon" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Night">
            <ItemTemplate>
                <asp:CheckBox ID="chkNyt" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Validate" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("CustomerId", typeof(int)), new DataColumn("Name", typeof(string)) });
        dt.Rows.Add(1, "Mudassar");
        dt.Rows.Add(2, "John");
        dt.Rows.Add(3, "Robert");
        this.Gridview1.DataSource = dt;
        this.Gridview1.DataBind();
    }
}
protected void Validate(object sender, EventArgs e)
{
    foreach (GridViewRow row in Gridview1.Rows)
    {
        CheckBox chkMorning = row.FindControl("chkMrng") as CheckBox;
        CheckBox chkAfterNoon = row.FindControl("chkNoon") as CheckBox;
        CheckBox chkNyt = row.FindControl("chkNyt") 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(), "CropImage", "alert('Atlease one checkbox should be selected in row " + (row.RowIndex + 1) + "');", true);
            break;
        }
    }
}

VB.Net

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("CustomerId", GetType(Integer)), New DataColumn("Name", GetType(String))})
        dt.Rows.Add(1, "Mudassar")
        dt.Rows.Add(2, "John")
        dt.Rows.Add(3, "Robert")
        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("chkMrng"), CheckBox)
        Dim chkAfterNoon As CheckBox = TryCast(row.FindControl("chkNoon"), CheckBox)
        Dim chkNyt As CheckBox = TryCast(row.FindControl("chkNyt"), 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](), "CropImage", "alert('Atlease one checkbox should be selected in row " & (row.RowIndex + 1) & "');", True)
            Exit For
        End If
    Next
End Sub

Screenshot