|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamic DropDownListHi.
(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.... 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" <TheH***@thehach.com> a écrit dans le message de news: Xns972E99F6E4BBDTheHachthehachcom@194.2.0.23...Show quote > 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....
Show quote
"Daniel TIZON" <daniel_tizon_pasdes***@hotmail.com> wrote in Thanks for your answer.news:ejd08TlAGHA.4080@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. > 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. 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" <TheH***@thehach.com> a écrit dans le message de news: Xns9731523535EE7TheHachthehachcom@194.2.0.23...Show quote > "Daniel TIZON" <daniel_tizon_pasdes***@hotmail.com> wrote in > news:ejd08TlAGHA.4080@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.
Show quote
"Daniel TIZON" <daniel_tizon_pasdes***@hotmail.com> wrote in This works fine !news:e$IKjJPBGHA.1312@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, > Thanks a lot ! |
|||||||||||||||||||||||