Solved

Make Sure All Check boxs are Checked in Gridview

Posted on 2016-08-22
4
70 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 9

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updating specific fields only in MVC/Entity controller 2 18
SSRS 2016 Rendering HTML tables 3 26
Change to file doesn't show up 16 52
Get month and date in a format 4 31
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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