Discussion:
How to quickly find control in DataGrid...
Gyorgy Bozoki
2007-10-16 04:39:01 UTC
Permalink
Hi all,

I have a DataGrid on a web page (ASP.Net 2.0) and I need to go through each
DataGridItem when a button is pressed on the page. The grid has 5 columns
and one of the columns has an HtmlTable control with a single row and 10
columns and each column contains a CheckBox control.

When the button is clicked on the page, I go through the grid (100 rows, for
example) and I need to access each checkbox to see if it changed. Right now
this is what I do:


DataGridItem oItem;
DataGridItemCollection oItems;
CheckBox chkCheckBox;
...
for ( int i = 0, lCount = oItems.Count; i < lCount; i++ )
{
oItem = oItems[i];

chkCheckBox = oItem.FindControl ( "ControlName" );
if ( chkCheckBox != null )
{
...
}
}


I assume that each row in the DataGrid has the same order of these
checkboxes (since it's generated from the HTML designer template), so I was
wondering if it was possible to somehow cache the control indices for my 10
checkboxes, since I found that FindControl() is a very expensive call.
(Checked in Reflector.)

The problem is that all these controls are the children of the immediate
HTML table cell in which they're placed, not the DataGridItem, so using an
index instead of the name-based lookup seems out of question (all checkboxes
have a 0 index, because they're the only controls in each table cell.)

Is there anything I could do to do away with the unnecessary lookups and
recursive string matches of FindControl? I feel like I'm missing something.

Thanks,
Gyorgy Bozoki

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
brian zinn
2007-10-17 17:18:31 UTC
Permalink
Gyorgy,

Since you haven't gotten a reply yet, maybe check-out MetaBuilders
SelectorField[1] control - it's open source. There are no FindControl
calls in the class itself, but I didn't bother checking it's base
class DataControlField or anything else, but maybe it's helpful.

Cheers,
Brian
Post by Gyorgy Bozoki
Hi all,
I have a DataGrid on a web page (ASP.Net 2.0) and I need to go through each
DataGridItem when a button is pressed on the page. The grid has 5 columns
and one of the columns has an HtmlTable control with a single row and 10
columns and each column contains a CheckBox control.
When the button is clicked on the page, I go through the grid (100 rows, for
example) and I need to access each checkbox to see if it changed. Right now
DataGridItem oItem;
DataGridItemCollection oItems;
CheckBox chkCheckBox;
...
for ( int i = 0, lCount = oItems.Count; i < lCount; i++ )
{
oItem = oItems[i];
chkCheckBox = oItem.FindControl ( "ControlName" );
if ( chkCheckBox != null )
{
...
}
}
I assume that each row in the DataGrid has the same order of these
checkboxes (since it's generated from the HTML designer template), so I was
wondering if it was possible to somehow cache the control indices for my 10
checkboxes, since I found that FindControl() is a very expensive call.
(Checked in Reflector.)
The problem is that all these controls are the children of the immediate
HTML table cell in which they're placed, not the DataGridItem, so using an
index instead of the name-based lookup seems out of question (all checkboxes
have a 0 index, because they're the only controls in each table cell.)
Is there anything I could do to do away with the unnecessary lookups and
recursive string matches of FindControl? I feel like I'm missing something.
Thanks,
Gyorgy Bozoki
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Mark Brackett
2007-10-18 14:01:46 UTC
Permalink
Yeah - you can find the first instance and backtrack through
Controls.IndexOf to find the appropriate indices. Something like:

DataGridItemCollection oItems = this.dataGrid.Items;

// Find location of table - assume we know the cell it's in
const int CELL_INDEX = 0;
HtmlTable table = (HtmlTable)
oItems[0].Cells[CELL_INDEX].FindControl("table");
int tableIndex = oItems[0].Cells[CELL_INDEX].Controls.IndexOf(table);

// Find location of our checkRow
HtmlTableRow checkRow = (HtmlTableRow) table.FindControl("checkRow");
int checkRowIndex = table.Controls.IndexOf(checkRow);

// Now we can index into .Controls instead of using FindControl
for (int i = 0; i < oItems.Count; i++) {
checkRow = (HtmlTableRow)
oItems[i].Cells[CELL_INDEX].Controls[tableIndex].Controls[checkRowIndex]
;
}

That being said - wouldn't CheckBoxList and SelectedIndexChanged work?

--MB
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Tuesday, October 16, 2007 12:39 AM
Subject: [DOTNET-WEB] How to quickly find control in DataGrid...
Hi all,
I have a DataGrid on a web page (ASP.Net 2.0) and I need to go through
each
DataGridItem when a button is pressed on the page. The grid has 5
columns
and one of the columns has an HtmlTable control with a single row and
10
columns and each column contains a CheckBox control.
When the button is clicked on the page, I go through the grid (100
rows, for
example) and I need to access each checkbox to see if it changed.
Right
now
DataGridItem oItem;
DataGridItemCollection oItems;
CheckBox chkCheckBox;
...
for ( int i = 0, lCount = oItems.Count; i < lCount; i++ )
{
oItem = oItems[i];
chkCheckBox = oItem.FindControl ( "ControlName" );
if ( chkCheckBox != null )
{
...
}
}
I assume that each row in the DataGrid has the same order of these
checkboxes (since it's generated from the HTML designer template), so
I
was
wondering if it was possible to somehow cache the control indices for
my 10
checkboxes, since I found that FindControl() is a very expensive call.
(Checked in Reflector.)
The problem is that all these controls are the children of the
immediate
HTML table cell in which they're placed, not the DataGridItem, so
using
an
index instead of the name-based lookup seems out of question (all
checkboxes
have a 0 index, because they're the only controls in each table cell.)
Is there anything I could do to do away with the unnecessary lookups
and
recursive string matches of FindControl? I feel like I'm missing
something.
Thanks,
Gyorgy Bozoki
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...