By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,354 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,354 IT Pros & Developers. It's quick & easy.

Dynamic DropDownList

P: n/a
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....
Dec 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
Daniel TIZON
MCP - MCSD.NET - MCT

"TheHach" <Th*****@thehach.com> a écrit dans le message de news:
Xn*******************************@194.2.0.23...
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....

Dec 16 '05 #2

P: n/a
"Daniel TIZON" <da********************@hotmail.com> wrote in
news:ej**************@TK2MSFTNGP14.phx.gbl:
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.
Dec 19 '05 #3

P: n/a
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,

--
Daniel TIZON
MCP - MCSD.NET - MCT

"TheHach" <Th*****@thehach.com> a écrit dans le message de news:
Xn*******************************@194.2.0.23...
"Daniel TIZON" <da********************@hotmail.com> wrote in
news:ej**************@TK2MSFTNGP14.phx.gbl:
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.

Dec 19 '05 #4

P: n/a
"Daniel TIZON" <da********************@hotmail.com> wrote in
news:e$**************@TK2MSFTNGP09.phx.gbl:
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 !
Dec 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.