Access controls in DataList

I have a DataList control on my page and inside are some dropdownlists.  When the first dropdownlist is changed I need to change other dropdowns as well.  I have been trying to access these controls like this:

Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        Dim dataItem As DataListItem = TryCast(ddl.NamingContainer, DataListItem)
        ddlFlokkur = TryCast(dataItem.FindControl("DataList1$ctl00$ddlFlokkur"), DropDownList)

The error I get when I try to work with ddlFlokkur is Object reference not set to an instance of an object.

How can I access dropdownlists in the datalist and work with them ?
TikkTakk-AiconAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
Hi,

You can try using a FindControlRecursive function to find the control that you want.

Usage (something like):  Dim ddlFlokkur as DropDownList = CType(FindControlRecursive(DataList,"ddlFlokkur"), DropDownList)


Public Shared Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
        If (root.ID = id) Then
            Return root
        End If
        For Each c As Control In root.Controls
            Dim t As Control = FindControlRecursive(c, id)
            If (Not (t) Is Nothing) Then
                Return t
            End If
        Next
        Return Nothing
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfred A.Commented:
Hi,

By the way, in my previous post, "id" in the parameter is the name of the control you are looking for and "root" is the parent control of the child control you are looking for.

I hope this helps.
0
TikkTakk-AiconAuthor Commented:
So if I´m looking for a dropdownlist that is in a datalist I should call the function like this ?

ddlFlokkur = FindControlRecursive(DataList1, "DataList1$ctl00$ddlFlokkur")
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Alfred A.Commented:
Yes.  Try it.  This will only work if "DataList1$ctl00$ddlFlokkur" is an existing control with that name inside DataList1.  Also, why are you using unique ID construct for the control (i.e. "DataList1$ctl00$ddlFlokkur")?  Would the control name of this inside your template would be ddlFlokkur inside the DataList1?  If this is the case, you don't need to add this DataList1$ctl00$.
0
TikkTakk-AiconAuthor Commented:
I tried this like this ddlFlokkur = FindControlRecursive(DataList1, "ddlFlokkur") and it seems to be working.  I´m still working on this and will check this further.  But so far so good.
0
Alfred A.Commented:
That is good to hear.  Goodluck! :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.