 |
|
 |
Thank you
However, it seems there is a small bug where you retrieve the data keys :
object DataKey = (this.Owner.DataKeys[selectedIndex].ToString());
As a data key can be of any type, you should not convert the key to string and simply do :
object DataKey = this.Owner.DataKeys[selectedIndex];
My 2 cents
|
|
|
|
 |
|
 |
Hi Dan_P,
I read your submission and it was very informative. Thanks to you for that. I tried testing the script on DataGrid in Windows Based Application instead of Internet ASP.NET and I could not get it to work. Could please help me out on Windows based application.
Thanks.
Have a Good Day
Lennie Kuah
|
|
|
|
 |
|
 |
i am going to use asp.net 1.1 how ia can browse a file in asp.net1.1
|
|
|
|
 |
|
 |
I like to load the project , build it and be able to modify it
I get an error about wanting to install web dev components. Anyway,
what type of project is it? Asp.net web app or asp.net control.
|
|
|
|
 |
|
 |
I have to do the same thing but in Visual Basic .net, does someone has it? thanks.
|
|
|
|
 |
|
 |
What will happen if more one page data will it work properly
|
|
|
|
 |
|
 |
Can't you already do this in a DataGrid using a TemplateColumn?
|
|
|
|
 |
|
 |
You can use <asp:TemplateColumn> tag
column under section in the datagrid
<asp:TemplateColumn>
<asp:CheckBox id="chkSelect" GroupName="grpSelect" runat="server">
Jegan
|
|
|
|
 |
|
 |
Hi, if we wanna add also one more checkbox to select all or unselect all, what should we do? I think this will help us writing such communication based pages like mails, i had this problem on my page, if u are interested i will be glad thanks
|
|
|
|
 |
|
 |
Ok.... I just went ahead and modified some code for everyone so that they can easily add a checkbox in the header which allows select/deselect all. This is server-side so if you have alot of traffic i would advise not to use this; instead use javascript. Whats nice about this code is that it is just a modification in the class and no where else.
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataGridControls
{
///
/// CheckBoxColumn Derives from DataGrid Column
///
public class CheckBoxColumn : DataGridColumn
{
public CheckBoxColumn(): base()
{
}
public override void InitializeCell(TableCell cell,
int columnIndex, ListItemType itemType)
{
//let the base class initialize the cell
base.InitializeCell(cell, columnIndex, itemType);
//add checkbox to header
if(itemType == ListItemType.Header)
{
CheckBox headerCheckBox = new CheckBox();
headerCheckBox.ID = "chkAll";
headerCheckBox.CheckedChanged += new EventHandler(this.headerCheckBox_CheckedChanged);
headerCheckBox.AutoPostBack = true;
cell.Controls.Add(headerCheckBox);
}
//now we add a checkbox to the rest of the cells
if( itemType == ListItemType.EditItem ||
itemType == ListItemType.Item ||
itemType == ListItemType.AlternatingItem ||
itemType == ListItemType.SelectedItem)
{
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
checkbox.Name = "checkboxCol";
checkbox.ID = "checkboxCol";
//assign an ID that we can use to find the control later
cell.Controls.Add(checkbox);
}
}
private void headerCheckBox_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridItem item in this.Owner.Items)
{
//iterate each DataGridItem and find our checkbox
HtmlInputCheckBox chkBox = (HtmlInputCheckBox) item.FindControl("checkboxCol");
//now set that checkboxCol value = to selected
if(chkBox.Checked == false)
chkBox.Checked = true;
else
chkBox.Checked = false;
}
}
public Int32[] SelectedIndexes
{
get
{
ArrayList selectedIndexList = new ArrayList();
//iterate each DataGridItem and find our checkbox
foreach( DataGridItem item in this.Owner.Items )
{
HtmlInputCheckBox chkBox =
(HtmlInputCheckBox) item.FindControl("checkboxCol");
//If it's selected then add it to our ArrayList
if ( chkBox != null && chkBox.Checked )
{
selectedIndexList.Add( item.ItemIndex );
}
}
return (Int32[])selectedIndexList.ToArray(typeof(
System.Int32 ) );
}
}
public object[] SelectedDataKeys
{
get
{
//Just iterate each of the selectedindexes and
//match it up to the datakey field
ArrayList dataKeyList = new ArrayList();
//make sure the datakeys have some values
if(this.Owner.DataKeys.Count > 0)
{
foreach( Int32 selectedIndex in SelectedIndexes )
{
object DataKey =
(this.Owner.DataKeys[selectedIndex].ToString());
dataKeyList.Add(DataKey);
}
}
return (object[])dataKeyList.ToArray(typeof( object ) );
}
}
}
}
Sully
|
|
|
|
 |
|
 |
If you check or uncheck some boxes then do a select/deselect all, it simply
flips the state of the checkboxes, that is not what I needed so...
I modified this function in order to set all check boxes to the header box state (all checked or all unchecked).
private void headerCheckBox_CheckedChanged(object sender, EventArgs e)
{
//grab the sender state here
CheckBox headerBox;
headerBox = (CheckBox) sender;
bool l_bState = headerBox.Checked;
foreach (DataGridItem item in this.Owner.Items)
{
//iterate each DataGridItem and find our checkbox
HtmlInputCheckBox chkBox = (HtmlInputCheckBox) item.FindControl("checkboxCol");
//now set the checkbox to the sender state
chkBox.Checked = l_bState;
}
}
//low
|
|
|
|
 |
|
 |
Hi there
for some reason I cannot get the postback to hit the checked event for the header...
Dunno What i am doing wrong.
Here is my code:
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
namespace DataGridControls
{
///
/// CheckBoxColumn Derives from DataGrid Column
///
public class CheckBoxColumn : DataGridColumn
{
public CheckBoxColumn()
: base()
{
}
private static String Name = "";
public String CheckBoxColumnName
{
get { return Name; }
set { Name = value; }
}
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
{
try
{
CheckBoxColumnName = this.Owner.ID;
}
catch (Exception ex)
{
}
//let the base class initialize the cell
base.InitializeCell(cell, columnIndex, itemType);
//add checkbox to header
if (itemType == ListItemType.Header)
{
CheckBox headerCheckBox = new CheckBox();
headerCheckBox.ID = CheckBoxColumnName + "_chkAll";
headerCheckBox.CheckedChanged += new EventHandler(this.headerCheckBox_CheckedChanged);
// headerCheckBox.AutoPostBack = true;
cell.Controls.Add(headerCheckBox);
}
//now we add a checkbox to the rest of the cells
if (itemType == ListItemType.EditItem ||
itemType == ListItemType.Item ||
itemType == ListItemType.AlternatingItem ||
itemType == ListItemType.SelectedItem)
{
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
checkbox.Name = CheckBoxColumnName + "_checkboxCol";
checkbox.ID = CheckBoxColumnName + "_checkboxCol" ;
//assign an ID that we can use to find the control later
cell.Controls.Add(checkbox);
}
}
private void headerCheckBox_CheckedChanged(object sender, EventArgs e)
{
//grab the sender state here
CheckBox headerBox;
headerBox = (CheckBox)sender;
bool l_bState = headerBox.Checked;
foreach (DataGridItem item in this.Owner.Items)
{
//iterate each DataGridItem and find our checkbox
HtmlInputCheckBox chkBox = (HtmlInputCheckBox)item.FindControl(CheckBoxColumnName + "_checkboxCol");
//now set the checkbox to the sender state
chkBox.Checked = l_bState;
}
}
public Int32[] SelectedIndexes
{
get
{
ArrayList selectedIndexList = new ArrayList();
//iterate each DataGridItem and find our checkbox
foreach (DataGridItem item in this.Owner.Items)
{
HtmlInputCheckBox chkBox =
(HtmlInputCheckBox)item.FindControl(CheckBoxColumnName + "_checkboxCol");
//If it's selected then add it to our ArrayList
if (chkBox != null && chkBox.Checked)
{
selectedIndexList.Add(item.ItemIndex);
}
}
return (Int32[])selectedIndexList.ToArray(typeof(
System.Int32));
}
}
public object[] SelectedDataKeys
{
get
{
//Just iterate each of the selectedindexes and
//match it up to the datakey field
ArrayList dataKeyList = new ArrayList();
//make sure the datakeys have some values
if (this.Owner.DataKeys.Count > 0)
{
foreach (Int32 selectedIndex in SelectedIndexes)
{
object DataKey =
(this.Owner.DataKeys[selectedIndex].ToString());
dataKeyList.Add(DataKey);
}
}
return (object[])dataKeyList.ToArray(typeof(object));
}
}
}
}
any ideas?
pretty please?
|
|
|
|
 |
|
 |
I think my problem is with the referencing. I'm not exactly sure where I'm supposed to place the class file. Should I start a new class library and place it in there?????
My assembly load trace
=== Pre-bind state information ===
LOG: DisplayName = DataGridCheckbox
(Partial)
LOG: Appbase = file:///c:/inetpub/wwwroot/donationnationCS
LOG: Initial PrivatePath = bin
Calling assembly : (Unknown).
===
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Post-policy reference: DataGridCheckbox
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/donationnationcs/baaf210b/9fa24547/DataGridCheckbox.DLL.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/donationnationcs/baaf210b/9fa24547/DataGridCheckbox/DataGridCheckbox.DLL.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/donationnationCS/bin/DataGridCheckbox.DLL.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/donationnationCS/bin/DataGridCheckbox/DataGridCheckbox.DLL.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/donationnationcs/baaf210b/9fa24547/DataGridCheckbox.EXE.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/donationnationcs/baaf210b/9fa24547/DataGridCheckbox/DataGridCheckbox.EXE.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/donationnationCS/bin/DataGridCheckbox.EXE.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/donationnationCS/bin/DataGridCheckbox/DataGridCheckbox.EXE.
Please help..... Thankyou
Sully
|
|
|
|
 |
|
 |
Hi
this class support 2 CHECKBOXCOLUMN ?
I wanna know if someone have added 2 CHECKBOXCOLUMN in the same datagrid ?
i got this error
Multiple controls with the same ID 'checkboxCol' were found. FindControl requires that controls have unique IDs.
______________
Olog-hai
Near to Mordor
hugues_gauthier@hotmail.com
|
|
|
|
 |
|
 |
I have to use Two datagrids within a single page, both with checkboxes. Is there any other way to use this control, that will get around the need to use Findcontrol?
With more than one checkbox column the findcontrol generates the error Olog-hai pointed out
|
|
|
|
 |
|
 |
Okay,
I add the project to my solution, create a reference to the dll, and add the @ Register directive at the top of my form. Runs great, and is so easy to use!
If I try to retreive the values as you have described I get a Data type undefined error. Why? I am using a code-behind. Is there something you need to do to make it work in a code-behind?
Thanks!
Dustin Boston
|
|
|
|
 |
|
 |
Hello,
I used your code in order to add a checkbox column to my datagrid.
So I would like to put in the header a checkbox to select/Deselectall.
I know put the checkbox in the header but i don't know how i have to program .
Thank you for your help.
Fred.
|
|
|
|
 |
|
 |
So, anyway to set the initial value of the checkbox based on the dataset you have??
|
|
|
|
 |
|
 |
i added the following inorder to set the inital values
in the webform.aspx added these lines
ArrayList selArray = new ArrayList();
foreach(DataRow dr in Ds.Tables[0].Rows)
{
selArray.Add(dr.ItemArray.GetValue(1));
}
second column in my datatable will have value "CHECK" or "UNCHECK" from my database.
in the web contorl i added one more method
public void setSelectedIndex(ArrayList selArray)
{
int indexchk = 0;
foreach( DataGridItem item in this.Owner.Items )
{
HtmlInputCheckBox chkBox = (HtmlInputCheckBox) item.FindControl("checkboxCol");
//If it's selected then add it to our ArrayList
if ( chkBox != null && selArray[indexchk].Equals("CHECK") )
chkBox.Checked = true;
else
chkBox.Checked = false;
indexchk++;
}
}
And i am able to achieve the result.
However I have little different problem now.
ny dataset will vary on each post back.
First time it checks accordingly but when i postback it doesn't change
The checkbox values are not changing and the initial values are retained.
can somebody help on this.
|
|
|
|
 |
|
 |
I am able to get the correct values if i set the checkboxes disabled.
But i dont want it that way. can any body help it.
|
|
|
|
 |
|
 |
because the chkbox are not allow in new enviorment.
So am I
|
|
|
|
 |
|
 |
I don't know what this guy is talking about. As long as you set your references, it works just fine. Also, this guy has poor communication skills
SNook
|
|
|
|
 |
|
 |
Hi,
Using your checkbox column object, is there a simple way to select or deselect all checkboxes in the column ?
Thanks,
Peter
|
|
|
|
 |
|
 |
i have setted the DataKeyField of DataGrid on desin view,then,when i view a page on IE,a error message say 'SelectedIndexes is only read', ......
|
|
|
|
 |
|
 |
Yeah, I had that too. You have to go into the HTML and edit the checkbox attributes to remove it.
Snook
|
|
|
|
 |