By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,385 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

CheckBox unchecked status detection in DataGrid Template Column

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.