Iterating through controls in .Net 2.0

D

Dave H

I was doing this in a .Net 1.1 site, but it doesn't work in 2.0, because the
objects aren't public (I think)

For k = 1 To iDDLCount
ddlObject = CallByName(oParent, sDDLName & k, CallType.Get)
ddlObject.Items.Clear()
Next

Any ideas?

Thanks, Dave
 
D

Dave H

I have a class that I put support code in, it's why I have to passs the
parent.

Say, on a page, I have 5 controls called 'DropDownList1'...'DropDownList5'

I use this code to set them, and fill them with thier defaults. I'll give
this a try..
 
W

Walter Wang [MSFT]

Thanks Steve for you informative input.

Hi Dave,

Based on my understanding, you question is how to use a generic method to
iterate child controls on a form and clear all DropDownList's items. If
I've misunderstood anything, please feel free to post here.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
ClearAllDropDownList(Me)
End Sub

Public Sub ClearAllDropDownList(ByVal parent As Control)
For Each ctl As Control In parent.Controls
If TypeOf ctl Is DropDownList Then
CType(ctl, DropDownList).Items.Clear()
Else
For Each subctl As Control In ctl.Controls
ClearAllDropDownList(subctl)
Next
End If
Next
End Sub

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Dave H

Thanks Walter.

My problem turned out to be the hierarchy of the control.

Using a MasterPage the parent was really the ContentControl... so I just
changed the code to:

oParent = CType(oParent.FindControl("ContentPlaceHolder1"),
ContentPlaceHolder)

Then it worked normally..

Maybe not the best way, but it got me past the problem..
 
W

Walter Wang [MSFT]

Hi Dave,

Thank you for your update.

Besides using your way to find the ContentPlaceHolder control and get the
child controls, I think using the recursive way to find the DropDownList
should work too since when compiled, the master page and the content page
will get merged.

You can verify this by turning on trace output of the content page:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2"
title="Untitled Page" Trace="true" %>

And view the page, you will see the control hierarchy such as:

__Page ASP.default2_aspx 1411 0 0
ctl00 ASP.masterpage_master 1411 0 0
ctl00$ctl02 System.Web.UI.LiteralControl 175 0 0
ctl00$ctl00 System.Web.UI.HtmlControls.HtmlHead 46 0 0
ctl00$ctl01 System.Web.UI.HtmlControls.HtmlTitle 33 0 0
ctl00$ctl03 System.Web.UI.LiteralControl 14 0 0
aspnetForm System.Web.UI.HtmlControls.HtmlForm 1156 0 0
ctl00$ctl04 System.Web.UI.LiteralControl 21 0 0
ctl00$ContentPlaceHolder1
System.Web.UI.WebControls.ContentPlaceHolder 746 0 0
ctl00$ContentPlaceHolder1$ctl00
System.Web.UI.LiteralControl 21 0 0
ctl00$ContentPlaceHolder1$DropDownList1
System.Web.UI.WebControls.DropDownList


So passing "Me" to the function I've written will correctly walk down all
the child controls.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top