• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

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 ?
0
TikkTakk-Aicon
Asked:
TikkTakk-Aicon
  • 4
  • 2
1 Solution
 
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
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now