Solved

Make Sure All Check boxs are Checked in Gridview

Posted on 2016-08-22
4
52 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 39

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

13 Experts available now in Live!

Get 1:1 Help Now