Assign a control to variable

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
 
L

Lucas Tam

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
 
M

MattB

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
 

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

Top