Link to home
Start Free TrialLog in
Avatar of TikkTakk-Aicon
TikkTakk-AiconFlag for Iceland

asked on

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 ?
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of TikkTakk-Aicon

ASKER

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")
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$.
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.
That is good to hear.  Goodluck! :-)