Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Iterate through the controls of a ItemTemplate

Reply
Thread Tools

Iterate through the controls of a ItemTemplate

 
 
Joe Abou Jaoude
Guest
Posts: n/a
 
      06-03-2007


Hi,
I have a datagrid with boudcolums and templatecolumns. The
templatecolums have a ItemTemplate that contains controls.
I would like to iterate through these controls. I cannot use the
FindControl method because I dont know the ID of the controls. I don't
even know the type of the controls. I m writting a generic function to
iterate throuh th controls and get back with their types, ID, Text ...

Surprisingly, I didn't find something that can allow me to do that.

Thank you

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-03-2007
You need to loop through the Controls collection.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Joe Abou Jaoude" <> wrote in message
news:...
>
>
> Hi,
> I have a datagrid with boudcolums and templatecolumns. The
> templatecolums have a ItemTemplate that contains controls.
> I would like to iterate through these controls. I cannot use the
> FindControl method because I dont know the ID of the controls. I don't
> even know the type of the controls. I m writting a generic function to
> iterate throuh th controls and get back with their types, ID, Text ...
>
> Surprisingly, I didn't find something that can allow me to do that.
>
> Thank you
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
 
 
 
Joe Abou Jaoude
Guest
Posts: n/a
 
      06-03-2007


Unfortunately, I m working at design time. I cannot loop through
controls collection because the collection is empty

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-03-2007
But you said you couldn't use FindControl because you didn't know the id.
How can you use FindControl in design time if you know the id?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


"Joe Abou Jaoude" <> wrote in message
news:%...
>
>
> Unfortunately, I m working at design time. I cannot loop through
> controls collection because the collection is empty
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
tcarternyc@gmail.com
Guest
Posts: n/a
 
      06-18-2007
Hi,

It sounds like you want to do something similar to what I have just
written. In my example dgDupManager is a datagrid. I wanted to iterate
the grid and find the controls buried in template columns (Item,
Alternating, Selected, etc) within each row of the grid. I used an
array list to make the controls easily accessible to me. The array
list in my example is called activeControls. Since I wanted to change
the values for these select controls I thought it would be easiser to
do with an array list because the grid had many controls that I do not
care to change.

for(int i=0;i<dgDupManager.Items.Count ;i++)
if (dgDupManager.Items[i].HasControls())
for(int n=0;n<dgDupManager.Items[i].Cells.Count ;n++)
if (dgDupManager.Items[i].Cells[n].HasControls())
foreach (Control c in dgDupManager.Items[i].Cells[n].Controls)
if (c.GetType().ToString().IndexOf("DropDownList")>0 ||
c.GetType().ToString().IndexOf("Button")>0)
//add a specific type of control into my array
list
activeControls.Add(c); //defined in my Page as ArrayList


The way that I parse my Array List and make changes to the control
values can be seen in this routine (which is executed when a button is
clicked). The goal in my case was to have the button change values for
selected controls between itself and the next button. I used another
trick of the datagrid to enable me to only place the buttons into an
artificially generated sub-header for the datagrid. The
assembleActiveControls method contains only the code above. The
activeControls[x] (see below) just allows me to go through the
controls that I care about without searching the datagrid. This kind
of functionality is good when you will build a datagrid that has
dynamically created controls or whose control id's are unknown to you.

//reference clicked button
System.Web.UI.WebControls.Button _button =
(System.Web.UI.WebControls.Button) sender;

//parse dropdownlist and button controls into an arraylist
assembleActiveControls();

//find location of the clicked button and reference next controls in
arraylist
int x = activeControls.LastIndexOf(_button) + 1;

//toggle the records in the clicked subheader
while(x < activeControls.Count &&
!
activeControls[x].GetType().ToString().Equals("System.Web.UI.WebCon trols.Button") )
{
DropDownList _temp = (DropDownList)activeControls[x];

//toggle the active controls based on the button clicked
if (_button.Text == "Process")
_temp.SelectedIndex = 1;
if (_button.Text == "Filter")
_temp.SelectedIndex = 0;
x++;
}

Hopefully, this will help someone since I just spent a weekend
thinking about the best way to handle this question. The above is not
completely finished code but hopefully its enough so that anyone
trying to do this in the future can get a good sense of how to
address.

On Jun 3, 8:40 am, Joe Abou Jaoude <anonym...@devdex.com> wrote:
> Hi,
> I have adatagridwith boudcolums and templatecolumns. The
> templatecolums have a ItemTemplate that contains controls.
> I would like toiteratethrough these controls. I cannot use the
> FindControl method because I dont know the ID of the controls. I don't
> even know the type of the controls. I m writting a generic function toiteratethrouh th controls and get back with their types, ID, Text ...
>
> Surprisingly, I didn't find something that can allow me to do that.
>
> Thank you
>
> *** Sent via Developersdexhttp://www.developersdex.com***



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to iterate 2 nested collections w <logic:iterate> without a"getter" John Java 4 04-01-2008 09:46 AM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
iterate through set of controls in code behind on postback =?Utf-8?B?T2xlZw==?= ASP .Net 2 07-27-2005 07:10 PM
can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate? Bennett Haselton ASP .Net 1 09-24-2004 01:59 AM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57