Assign a control to variable

  • Thread starter Thread starter MattB
  • Start date Start date
M

MattB

I've got an form (asp/vb.net) that might need a Checkboxlist or a
RadioButtonList, depending on some data in the database.
My implementation plan is this: have both controls on the page, but with
Visible=False. Once I can tell if I'll need the CheckBoxList or
RadioButtonList, I'd like to be able to assign the control to a
variable, like this:
-----

Dim myControl as System.Web.control

If bCanMultiSelect Then
myControl = myCheckBoxList
Else
myControl = myRadioButtonList
End If

With myControl
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With

-----

As you might guess, Intellisense doesn't like the myControl variable
being assigned like that and won't let me use it this way. Is there
something similar to get the desired results of having one reference
that could be wither type of control so I don't have to double all my
code setting and retrieving values from this control?
Thanks!

Matt
 
Dim myControl as System.Web.control

If bCanMultiSelect Then
myControl = myCheckBoxList
Else
myControl = myRadioButtonList
End If

With myControl
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With

-----

As you might guess, Intellisense doesn't like the myControl variable
being assigned like that and won't let me use it this way. Is there
something similar to get the desired results of having one reference
that could be wither type of control so I don't have to double all my
code setting and retrieving values from this control?
Thanks!

Easiest would be go:

If bCanMultiSelect Then
myControl = myCheckBoxList

With Ctype(myControl,CheckBoxList)
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With


Else
myControl = myRadioButtonList

With Ctype(myControl,RadioButtonList)
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With

End If
 
Lucas said:
@individual.net:




Easiest would be go:

If bCanMultiSelect Then
myControl = myCheckBoxList

With Ctype(myControl,CheckBoxList)
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With


Else
myControl = myRadioButtonList

With Ctype(myControl,RadioButtonList)
.Visible = True
.DataSource = dvFilteredList
.DataTextField = "descrip"
.DataValueField = "item"
.DataBind()
End With

End If


Thanks!

Matt
 
Back
Top