Re-using DropDown Lists?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All

I have a question about re-using a drop downlist box across several
questions on a questionaire like form. I will include code after some
explaination.

I thought i could make a dropdownlist box object global and re-use it as
needed if i did not destroy the object, but it doesnt seem to be working out
for me.

In the Class file i declare the object as follows:
Private ddlListBoxMulti As DropDownList

On Page Load i call the Sub to create the DropDownList Box

Get_MultiDDL()

I have changed this proceedure from a sub to function, but previously it was
a sub that created the DropDown List box:

Public Sub Get_MultiDDL()
'(a Note: SQLHelper is imported from another Assembly and is used as a
datareader)

Dim drDDLValues As SqlHelper
Dim drDDLValuesList As SqlDataReader
Dim i As Integer = 0
ddlListBoxMulti = New DropDownList

i = 0

'Call DB for Values needed
drDDLValuesList =
drDDLValues.ExecuteReader(ConfigurationSettings.AppSettings("SQLServer"),
CommandType.StoredProcedure, "csp_SELECT_Answer_Multi")
'Add first item blank
ddlListBoxMulti.Items.Add(" ")
ddlListBoxMulti.Items(i).Value() = 0

i += 1
'Loop to get all values needed into DropDownList
Do While drDDLValuesList.Read()
ddlListBoxMulti.Items.Add(drDDLValuesList.GetString(1))
ddlListBoxMulti.Items(i).Value() = drDDLValuesList.GetInt32(0)
i += 1
Loop

i = 0


drDDLValuesList.Close()
drDDLValuesList = Nothing
drDDLValues = Nothing

End sub


Then, i have to construct a table of twenty questions, the first twenty are
an intial interview, then a second group of twenty for a thirty day
evaluation, all nicely tucked into table cells. For brevity, i will only
include the section of code that is in question.


What i am trying to accomplish is to make one call to the function and
re-use the DropDownList box everytime i need it as follows and remeber for
brevity i only toook out the code in question, this code appears in a loop
that gets the questions from
the SQLDB:
cell = New TableCell
'******this code does not work
cell.Controls.Add(ddlListBoxMulti)
ddlListBoxMulti.ID = "ddlMIntial" & i.ToString()
ddlListBoxMulti.Width = Unit.Pixel(150)
cell.Attributes.Add("align", "center")
row.Cells.Add(cell)

cell = New TableCell
cell.Controls.Add(Get_MultiDDL())
ddlListBoxMulti.ID = "ddlM30Day" & i.ToString()
ddlListBoxMulti.Width = Unit.Pixel(150)
cell.Attributes.Add("align", "center")
row.Cells.Add(cell)


I realize this is long but why does the DropDownlist seem to disappear and
not get rendered using the second method

Any help greatly appreciated

TIA
 
Maybe I'm missing something, but if you want to get 20 cells with
DropDownList in each you need to create 20 DropDownLists. What you're doing
is you're creating a new DropDownList in your Get_MultiDDL(), but you're
assigning it to a global variable. The only thing you're reusing is the
ddlListBoxMulti pointer. The next time you call your Sub the ddlListBoxMulti
gets the new object and the old object is lost. So when it comes time to
render you will only have your last DropDownList.

If you want to reuse the logic of creating DropDownLists make your
Get_MultiDDL() a function returning DropDownList:

Public Function Get_MultiDDL() as DropDownList

Dim drDDLValues As SqlHelper
Dim drDDLValuesList As SqlDataReader
Dim i As Integer = 0
Dim ddl as New DropDownList

i = 0

'Call DB for Values needed
drDDLValuesList =
drDDLValues.ExecuteReader(ConfigurationSettings.AppSettings("SQLServer"),
CommandType.StoredProcedure, "csp_SELECT_Answer_Multi")
'Add first item blank
ddl.Items.Add(" ")
ddl.Items(i).Value() = 0

i += 1
'Loop to get all values needed into DropDownList
Do While drDDLValuesList.Read()
ddl.Items.Add(drDDLValuesList.GetString(1))
ddl.Items(i).Value() = drDDLValuesList.GetInt32(0)
i += 1
Loop

i = 0


drDDLValuesList.Close()
drDDLValuesList = Nothing
drDDLValues = Nothing

return ddl

End Function


Then you can simply do cell.Controls.Add(Get_MultiDDL()) in your second loop
 
Hi Thanks For your answer. I had a;ready changed it to what you suggested
before reading your post.

The Reason i wanted one DropDownList is because all 20 questions can be
answered by the contents od the DDL

so i was trying to reuse it over and over again.

Silly me, I did figure it out and i do thank you for your help!! Sometimes
you just shouldn't code on a 24 hour Diet Coke Buzz!!
 
Back
Top