?
Solved

Make Sure All Check boxs are Checked in Gridview

Posted on 2016-08-22
4
Medium Priority
?
95 Views
Last Modified: 2016-08-23
I have a gridview that in the last column has a checkbox column(chkLiveDiscussed).    The number of rows in the gridview could be anything between 2 to 20

I have a checkbox outside of the grid view(chkAllDiscussed) that i ONLY want enabled when all of the rows i the gridview have the checkbox(chkLiveDiscussed) checked.  How can I achieve this?

   <asp:GridView ID="gvLiveLearners" runat="server" DataSourceID="dsLiveLeaners" AutoGenerateColumns="False" CssClass="tableAc" DataKeyNames="PROGRESS_RAG" BorderColor="#B2CB96" BorderStyle="Dotted" BorderWidth="1px">
                        <Columns>
                            <asp:BoundField DataField="FULLNAME" HeaderText="FULLNAME" SortExpression="FULLNAME" />
                            <asp:BoundField DataField="SECTOR" HeaderText="Route" SortExpression="SECTOR" />
                            <asp:BoundField DataField="SCHEME" HeaderText="Level" SortExpression="SCHEME" />
                            <asp:BoundField DataField="LATEST_REVIEW" HeaderText="Last Review" ReadOnly="True" SortExpression="LATEST_REVIEW" DataFormatString="{0:d}" />
                            <asp:BoundField DataField="AptStartTime" HeaderText="Next Review" SortExpression="AptStartTime" DataFormatString="{0:d}" />
                            <asp:BoundField DataField="NextAptWith" HeaderText="NextAptWith" ReadOnly="True" SortExpression="NextAptWith" Visible="False" />
                            <asp:BoundField DataField="NUM_REVIEWS" HeaderText="Num Reviews" ReadOnly="True" SortExpression="NUM_REVIEWS" HeaderStyle-CssClass="hidden" ItemStyle-CssClass="hidden" />
                            <asp:BoundField DataField="MonthOnPOT" HeaderText="MonthOnPOT" ReadOnly="True" SortExpression="MonthOnPOT" />
                            <asp:BoundField DataField="MonthsOnProgramme" HeaderText="Months on Program" ReadOnly="True" SortExpression="MonthsOnProgramme" />
                            <asp:BoundField DataField="EARLIEST_START_DATE" HeaderText="EARLIEST_START_DATE" ReadOnly="True" SortExpression="EARLIEST_START_DATE" DataFormatString="{0:d}" Visible="False" />
                            <asp:BoundField DataField="POTSTATUS" HeaderText="POTSTATUS" SortExpression="POTSTATUS" />
                            <asp:BoundField DataField="PROGRESS_RAG" HeaderText="PROGRESS_RAG" ReadOnly="True" SortExpression="PROGRESS_RAG" >
<HeaderStyle CssClass="hidden"></HeaderStyle>

<ItemStyle CssClass="hidden"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="CURRENT_PDM" HeaderText="CURRENT_PDM" SortExpression="CURRENT_PDM" />
                            <asp:BoundField DataField="CONTRACT" HeaderText="CONTRACT" SortExpression="CONTRACT" Visible="False" />
                            <asp:BoundField DataField="TRAINEEID" HeaderText="TRAINEEID" ReadOnly="True" SortExpression="TRAINEEID" Visible="False" />
                            <asp:BoundField DataField="POT" HeaderText="POT" ReadOnly="True" SortExpression="POT" Visible="False" />
                            <asp:BoundField DataField="POTSTATUSID" HeaderText="POTSTATUSID" SortExpression="POTSTATUSID" Visible="False" />
                            <asp:TemplateField HeaderText="Discussed">
                              
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkLiveDiscussed" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                   
                           <span class="auto-style5">There are no live learners at this branch</span>
                        </EmptyDataTemplate>
                        <HeaderStyle BackColor="#AEC785" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <RowStyle BorderStyle="Dashed" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:GridView>   <asp:CheckBox ID="chkAllDiscussed" Enabled="False"  runat="server" />

Open in new window



Thanks
0
Comment
Question by:Ed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Expert Comment

by:Prakash Samariya
ID: 41765384
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41765771
This expert suggested creating a Gigs project.
add an javascript checked changed to your checkbox pointing at this function.

function check_changed()
{
  var allChecked = true;
    $('#<%= gvLiveLearners.ClientID%>').find('tr').each(function () {
        var row = $(this);
        if (!row.find('input[type="checkbox"]').is(':checked'){
                  allchecked =false;
                   return false;
        }
    });
   alert(allChecked);
}
    

    

Open in new window


Please disregard the gigs recommendation, was clicked on accident.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 41765933
Another solution:

Default.aspx -
<%@ Page Title="Home Page" Language="VB" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="EE_Q28964699._Default" %>

<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
		<Columns>
			<asp:BoundField DataField="ID" HeaderText="ID" />
			<asp:BoundField DataField="FullName" HeaderText="Full Name" />
			<asp:BoundField DataField="Age" HeaderText="Age" />
			<asp:TemplateField HeaderText="Is Working?">
				<ItemTemplate>
					<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%#DataBinder.GetPropertyValue(Container.DataItem, "IsWorking")%>' OnCheckedChanged="OnCheckedChanged" AutoPostBack="True" />
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
	<asp:CheckBox ID="CheckBox1" runat="server" Text="All Persons Working?" />
</form>

Open in new window

Default.aspx.vb -
Public Class _Default
	Inherits Page

	Protected Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			GridView1.DataSource = (From i In Enumerable.Range(0, 20) Select New Person() With {.ID = i, .FirstName = String.Format("FirstName{0}", i), .MiddleName = String.Format("MiddleNmae{0}", i), .LastName = String.Format("LastName{0}", i), .Birthdate = DateTime.Now.AddYears(-(9 * i)), .IsWorking = i Mod 2 = 0}).ToList()
			GridView1.DataBind()
		End If
	End Sub

	Protected Sub OnCheckedChanged(sender As Object, e As EventArgs)
		If GridView1 IsNot Nothing AndAlso GridView1.Rows.Count > 0 Then
			CheckBox1.Checked = GridView1.Rows.Cast(Of GridViewRow).Where(Function(r) CType(r.Cells(3).FindControl("CheckBox2"), CheckBox).Checked).Count = GridView1.Rows.Count
		End If
	End Sub
End Class

<Serializable()> _
Class Person
	Public Property ID() As Integer
	Public Property FirstName() As String
	Public Property MiddleName() As String
	Public Property LastName() As String
	Public Property Birthdate() As DateTime
	Public Property IsWorking() As Boolean

	Public ReadOnly Property FullName() As String
		Get
			Return String.Format("{0} {1}. {2}", FirstName, MiddleName.First(), LastName)
		End Get
	End Property

	Public ReadOnly Property Age() As String
		Get
			Return String.Format("{0} years old", DateTime.Now.Year - Birthdate.Year)
		End Get
	End Property
End Class

Open in new window

Default.aspx.designer.vb -
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated. 
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Partial Public Class _Default

	'''<summary>
	'''form1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

	'''<summary>
	'''GridView1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents GridView1 As Global.System.Web.UI.WebControls.GridView

	'''<summary>
	'''CheckBox1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents CheckBox1 As Global.System.Web.UI.WebControls.CheckBox
End Class

Open in new window


Produces the following output -

Initial load:Capture.JPGAfter checking all IsWorking checkboxes:Capture.JPGAfter unchecking one of the previously selected checkboxes:Capture.JPG
-saige-
0
 

Author Closing Comment

by:Ed
ID: 41766562
Perfect Solution
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question