Solved

Make Sure All Check boxs are Checked in Gridview

Posted on 2016-08-22
4
59 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:edjones1
4 Comments
 
LVL 8

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 33

Accepted Solution

by:
it_saige earned 500 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:edjones1
ID: 41766562
Perfect Solution
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now