473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CheckBox unchecked status detection in DataGrid Template Column

Hi Everyone,

I have a dynamic checkbox in a datagrid that uses the ITemplate interface
and has the checkchanged event wired up. When the checkbox is checked, the
event
event handler that handles the event gets called, however when it is
unchecked, it does not fire. Is this by design, or is something else that
is going on? Thanks!!.

This message was originally posted by ma****@discussions.microsoft.com, but
since she hasn't get any response and I'm experiencing the same issue, I'm
posting it here.

Regards,
Javier.
Dec 12 '05 #1
2 2689
Post some snippet code and it would be easier to see what you are trying to
do
Patrick

"Javier" <jt*******@htp.bcit.ca> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
Hi Everyone,

I have a dynamic checkbox in a datagrid that uses the ITemplate interface
and has the checkchanged event wired up. When the checkbox is checked, the event
event handler that handles the event gets called, however when it is
unchecked, it does not fire. Is this by design, or is something else that
is going on? Thanks!!.

This message was originally posted by ma****@discussions.microsoft.com, but since she hasn't get any response and I'm experiencing the same issue, I'm
posting it here.

Regards,
Javier.

Dec 12 '05 #2
Hi Patrick,
Ok, here's the code.

This is the template column that hosts the header and items checkboxes
:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Specialized;
namespace ASPnetControls.WebControls
{
public class CheckBoxTemplateColumn : TemplateColumn
{
private string headerCheckBoxTooltip;
private CustomCheckBoxHeader headerCheckBox;
private CustomCheckBoxItem itemCheckBox;

public string CheckBoxHeaderTooltip
{
set{this.headerCheckBoxTooltip = value;}
}

public override void Initialize()
{
headerCheckBox = (headerCheckBoxTooltip != null)? new
CustomCheckBoxHeader(headerCheckBoxTooltip) : new CustomCheckBoxHeader();
base.HeaderTemplate = headerCheckBox;
itemCheckBox = new CustomCheckBoxItem();
base.ItemTemplate = itemCheckBox;
itemCheckBox.PreRender = new EventHandler(itemCheckBox_PreRender);
base.Initialize();
}

private void itemCheckBox_PreRender(object sender, EventArgs e)
{
CustomCheckBoxItem itemCheckBox = (CustomCheckBoxItem)sender;
itemCheckBox.JsHeaderArray = headerCheckBox.JsHeaderArray;
itemCheckBox.JsItemsArray = headerCheckBox.JsItemsArray;
}

// internal member
private abstract class CustomCheckBox : CheckBox, ITemplate
{
protected const string JavaScriptBlockFormatter = "<script
language=\"javascript\">{0}</script>";
protected string jsHeaderArray, jsItemsArray;

#region ITemplate Members
public virtual void InstantiateIn(Control container)
{
container.Controls.Add(this);
}
#endregion
}

// internal member
private class CustomCheckBoxHeader : CustomCheckBox
{
public CustomCheckBoxHeader(string tooltip)
{
this.Attributes.Add("title", tooltip);
}

public CustomCheckBoxHeader(){}

public string JsHeaderArray
{
get{return base.jsHeaderArray;}
}

public string JsItemsArray
{
get{return base.jsItemsArray;}
}

protected override void OnPreRender(EventArgs e)
{
base.jsHeaderArray = this.ClientID;
base.jsItemsArray = base.jsHeaderArray + "_";
this.Page.RegisterArrayDeclaration(base.jsHeaderAr ray,
String.Format("'{0}'", jsHeaderArray));
this.Page.RegisterClientScriptBlock("InitializeHea derCheckBox",
String.Format(JavaScriptBlockFormatter, @"function InitializeHeaderCheckBox
(itemsArray, headerArray)
{
controllerRef = document.getElementById(headerArray[0]);
controllerRef.onclick = function()
{
for(i=0; itemsArray.length > i; ++i)
{
controlleeRef = document.getElementById(itemsArray[i]);
if(!controlleeRef.disabled)
controlleeRef.checked = this.checked;
};
}
checkedCount = 0;
disabledCount = 0;
for(i=0; itemsArray.length > i; ++i)
{
controlleeRef = document.getElementById(itemsArray[i]);
if(controlleeRef.checked)
checkedCount++;
if(controlleeRef.disabled)
disabledCount++;
}
controllerRef.disabled = disabledCount == itemsArray.length;
controllerRef.checked = checkedCount == itemsArray.length;
}"));
this.Page.RegisterStartupScript(base.jsHeaderArray ,
String.Format(JavaScriptBlockFormatter,
String.Format("InitializeHeaderCheckBox({0},{1});" ,
base.jsItemsArray, base.jsHeaderArray)));
}
}

// internal member
private class CustomCheckBoxItem : CustomCheckBox
{
public new EventHandler PreRender;
public string JsHeaderArray
{
set{base.jsHeaderArray = value;}
}

public string JsItemsArray
{
set{base.jsItemsArray = value;}
}

public override void InstantiateIn(Control container)
{
CustomCheckBoxItem cb = new CustomCheckBoxItem();
cb.PreRender = this.PreRender;
container.Controls.Add(cb);
}

protected override void OnPreRender(EventArgs e)
{
PreRender(this, e);
this.Page.RegisterArrayDeclaration(base.jsItemsArr ay,
String.Format("'{0}'", this.ClientID));
this.Page.RegisterClientScriptBlock("InitializeIte msCheckBoxes",
String.Format(JavaScriptBlockFormatter, @"function InitializeItemsCheckBoxes
(itemsArray, headerArray)
{
var CheckBoxArrayTester = function(s)
{
for(i=0; s.length > i; ++i)
{
controlleeRef = document.getElementById(s[i]);
if (controlleeRef.checked)
continue;
else
return true;
}
}
for(i=0; itemsArray.length > i; ++i)
{
document.getElementById(itemsArray[i]).onclick = function()
{
controllerRef = document.getElementById(headerArray[0]);
if (this.checked && CheckBoxArrayTester(itemsArray) != true)
controllerRef.checked = true;
else if (!this.checked && CheckBoxArrayTester(itemsArray))
controllerRef.checked = false;
}
}
}"));
this.Page.RegisterStartupScript(base.jsItemsArray,
String.Format(JavaScriptBlockFormatter,
String.Format("InitializeItemsCheckBoxes({0},{1}); ",
base.jsItemsArray, base.jsHeaderArray)));
}
}
}
}

-------------------------- Page class that hosts the datagrid with the
template
column ------------------------------------------------------------------------------
aspx file ->

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false"
Inherits="ContentManager.WebForm2" %>
<%@ Register TagPrefix="aspnetw" Namespace="ASPnetControls.WebControls"
Assembly="ASPnetControls"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" DataSource='<%# new
string[]{"a","b","c","d","e","f","g","h","i"} %>'
AutoGenerateColumns="True">
<Columns>
<aspnetw:CheckBoxTemplateColumn/>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 648px; POSITION:
absolute; TOP: 24px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>

cs file ->

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ContentManager
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------
Everything works fine, except for keeping the state and getting called the
event handler only when a checkbox is set to unchecked after having been
checked.

Thanks in advance,
Javier.


"Patrick.O.Ige" <na********@toughguy.net> wrote in message
news:eN*************@tk2msftngp13.phx.gbl...
Post some snippet code and it would be easier to see what you are trying
to
do
Patrick

"Javier" <jt*******@htp.bcit.ca> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
Hi Everyone,

I have a dynamic checkbox in a datagrid that uses the ITemplate interface
and has the checkchanged event wired up. When the checkbox is checked,

the
event
event handler that handles the event gets called, however when it is
unchecked, it does not fire. Is this by design, or is something else
that
is going on? Thanks!!.

This message was originally posted by ma****@discussions.microsoft.com,

but
since she hasn't get any response and I'm experiencing the same issue,
I'm
posting it here.

Regards,
Javier.


Dec 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Randy | last post by:
Hello, I'm creating a table on the fly that is used by a datagrid. I'm also creating a tableStyle that is used for the datagrid to make it look like I want. I'm using the DataGridBoolColumn to...
2
by: michael walser | last post by:
display checkbox in datagrid. I find that there are 3 status: checked - check box with click icon unchecked - blank in check box unknown - click icon but color in gray..... How can I set the...
0
by: Just D | last post by:
Hi All, Does anybody know how to write the following code? I have a database table with a few columns including: ID , "Task" , ForceRun . I need to show the DataGrid on the ASPX page with...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
1
by: iforsyth | last post by:
Have a paging datagrid with a checkbox control in column(0). ViewState is enabled. I check the checkbox in first row of the grid on a page and then the program hits this event: Private Sub...
1
by: Machi | last post by:
Let say i select rows of records with 4 columns from Database and want to display the data in DataGrid (ASP.NEt Server Control) with one column which must be displayed in CheckBox layout. For the...
1
by: Chen Sun via .NET 247 | last post by:
I have a datagrid with a checkbox column, if i check onecheckbox, and then change to another page, when i go back to theprevious page, the checkbox that i have marked is uncheckedagain, is there...
10
by: Jennyfer J Barco | last post by:
Hello, I have a datagrid that brings some information from a query. I need to have a checkbox in each row so the user can select the rows he wants to reprint. Is it possible to have a checkbox...
6
by: Bill Nguyen | last post by:
I need to add a checkbox in front of all the rows in a datagrid so that users can select/unselect them. Any help is greatly appreciated! Bill
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.