Dynamic DropDownList

T

TheHach

Hi.

(For information, i'm working in VB.NET)
I have a datagrid on my page.
On its creation, I add a dropdownlist in each cell, with a different ID
each time.
This works fine.

But now, I can't access on the selected values of those dynamic
dropdownlists.
I tried this :
CType(Me.FindControl("ddlDay" & i), DropDownList).SelectedValue
But this generates me an error that says that this control doesn't
exists...
What am I doing wrong ???

Thanks for your help....
 
D

Daniel TIZON

Hi,
Your problem comes from the FindControl method wich is not recursive.
The best method shoud be to create a common eventHandler for all yours
dropdownlists, for example : DropDownLists_SelectedIndexChanged
and add the handler dynamicaly, with the following code when you add the
dropdownlists in the cells
code :
dim myDropdown as new DropDownList()
myDropdown.ID=ddlDay" & i
e.item.cells(0).add(myDropdown)
AddHandler myDropdown.SelectedIndexChanged, AddressOf
DropDownLists_SelectedIndexChanged

If the dropdownlist where added in a template column, it whould be easier,
the association whould be made declaratively with the OnSelectedIndexChanged
attribute.
 
T

TheHach

Hi,
Your problem comes from the FindControl method wich is not recursive.
The best method shoud be to create a common eventHandler for all yours
dropdownlists, for example : DropDownLists_SelectedIndexChanged
and add the handler dynamicaly, with the following code when you add
the dropdownlists in the cells
code :
dim myDropdown as new DropDownList()
myDropdown.ID=ddlDay" & i
e.item.cells(0).add(myDropdown)
AddHandler myDropdown.SelectedIndexChanged, AddressOf
DropDownLists_SelectedIndexChanged

If the dropdownlist where added in a template column, it whould be
easier, the association whould be made declaratively with the
OnSelectedIndexChanged attribute.

Thanks for your answer.
But your solution is only to add an event on the dynamic dropdownlists,
isn't it ?
My problem is that I have a datagrid with one column for each day of the
selected month, and a dropdownlist in each column. And when I click on
the submit button, I want to recover the selected value from each
dropdownlist. And I think your solution won't help me for this.
Do you have another one ??
Thanks in advance.
 
D

Daniel TIZON

Hi,
Sorry to have misunderstood your scenario.
You need to get all the references of all yours Dropdownlist to tests their
selected value when you click on a button of the page.
All the references of the dropdownlist are reachable in the treecontrols of
the page. You can browse recusrsively the controls ins the page or in the
DataGrid and play with the controls of type DropDownlist. This must work,
but is not the most effective.
Another solution is to create your own collection of DropdDownLists :
- declare a member variable of type ArrayList in the class of your Page
- create the instance in the Page_Init
- In the event where you creates the Dropdownlist controls, probably in
DataGrid1_ItemCreated, add the ComboBox to the collection of the ArrayList
- In the Click EventHandler of the button, use a For each loop to play with
the list of your dropdownlists.

Hope this helps,
 
T

TheHach

Hi,
Sorry to have misunderstood your scenario.
You need to get all the references of all yours Dropdownlist to tests
their selected value when you click on a button of the page.
All the references of the dropdownlist are reachable in the
treecontrols of the page. You can browse recusrsively the controls ins
the page or in the DataGrid and play with the controls of type
DropDownlist. This must work, but is not the most effective.
Another solution is to create your own collection of DropdDownLists :
- declare a member variable of type ArrayList in the class of your
Page - create the instance in the Page_Init
- In the event where you creates the Dropdownlist controls, probably
in DataGrid1_ItemCreated, add the ComboBox to the collection of the
ArrayList - In the Click EventHandler of the button, use a For each
loop to play with the list of your dropdownlists.

Hope this helps,

This works fine !
Thanks a lot !
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top