We help IT Professionals succeed at work.

Help with error retreive control's text value

vcharles
vcharles asked
on
Hello,

The code below given to me works but instead of retreiving the Text value, it retreives the ID value of my DropDownList control.

   Dim ctrl As Control = MultiView1.FindControl("C1" & curXYZ)

            Dim Txt As String
            If TypeOf (ctrl) Is DropDownList Then
                Txt = CType(ctrl, DropDownList).Text
            ElseIf TypeOf (ctrl) Is C1MaskedInput Then
                Txt = CType(ctrl, C1MaskedInput).Text
            ElseIf TypeOf (ctrl) Is C1ComboBox Then
                Txt = CType(ctrl, C1ComboBox).SelectedItem.Text
            ElseIf TypeOf (ctrl) Is TextBox Then
                Txt = CType(ctrl, TextBox).Text
            End If

When I try  Txt = CType(ctrl, DropDownList).Text, I receive an error message "Object not set to a instance of an object"

What is the proper syntax to retreive the Text value of the data selected in the DropdownList?

Victor
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014
Commented:
The error means that the control is null. Try the SelectedValue property

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.text.aspx

Author

Commented:
I'm getting very strange results, my dropdownlist is loaded from the page load event with the following code:

  Dim Fuze As New DataSet
            Receiver.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            C1Receiver.DataSource = ReceiverTables(0)
            C1Receiver.DataMember = "Receiver"
            C1Receiver.DataTextField = "Receiver"
            C1Receiver.DataValueField = "Receiver_ID"
            C1Receiver.DataBind()

When I set C1Receiver.DataValueField = "Receiver_ID"

1. C1Receiver.Text = 2
2. C1Receiver.SelectedValue = 2
3. C1Receiver.SelectedItem.Text = 2

But When I set C1Receiver.DataValueField = "Receiver"

I get The Text value (i.e. Bel) for all three settings listed above.

I don't understand why I'm getting the ID value for the 1st and 3rd commands above.

Any ideas what is wrong?

Author

Commented:
Correction with my Dataset statement.

I'm getting very strange results, my dropdownlist is loaded from the page load event with the following code:

  Dim Receiver As New DataSet
            Receiver.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            C1Receiver.DataSource = ReceiverTables(0)
            C1Receiver.DataMember = "Receiver"
            C1Receiver.DataTextField = "Receiver"
            C1Receiver.DataValueField = "Receiver_ID"
            C1Receiver.DataBind()

When I set C1Receiver.DataValueField = "Receiver_ID"

1. C1Receiver.Text = 2
2. C1Receiver.SelectedValue = 2
3. C1Receiver.SelectedItem.Text = 2

But When I set C1Receiver.DataValueField = "Receiver"

I get The Text value (i.e. Bel) for all three settings listed above.

I don't understand why I'm getting the ID value for the 1st and 3rd commands above.

Any ideas what is wrong?
Most Valuable Expert 2012
Top Expert 2014

Commented:
The reason is stated in the link I posted above

The Text property gets and sets the same value that the SelectedValuee property does.

Not sure why SelectedItem.Text would return ID though.

Author

Commented:
I was able to see the text entered when I used a message box with the following code:
C1Receiver.SelectedItem.Text

But Since I'm trying to pass the text value to the Txt variable, I'm still getting the ID value, Txt = 2 instead of the Text value. I can not use Txt.Text because Text is not a property for Txt.

How do I retreive the text value selected in a dropdownlist using Txt?

Author

Commented:
I am using the code below to retreive the text value using the Txt variable.

Dim ctrl As Control = MultiView1.FindControl("C1" & curXYZ)

            Dim Txt As String
            If TypeOf (ctrl) Is DropDownList Then
                Txt = CType(ctrl, DropDownList).Text
            ElseIf TypeOf (ctrl) Is C1MaskedInput Then
                Txt = CType(ctrl, C1MaskedInput).Text
            ElseIf TypeOf (ctrl) Is C1ComboBox Then
                Txt = CType(ctrl, C1ComboBox).SelectedItem.Text
            ElseIf TypeOf (ctrl) Is TextBox Then
                Txt = CType(ctrl, TextBox).Text
            End If

Most Valuable Expert 2012
Top Expert 2014

Commented:
>I was able to see the text entered when I used a message box with the following code:
>C1Receiver.SelectedItem.Text

Is C1Receiver a dropdownlist or a C1Combobox? What is the type of dropdownlist.SelectItem? Is it datarowview?

Author

Commented:
Hello,

It worked with selectedItem.Text, I am using a dropdownlist until i resolve some issues with the combo box.

Thanks,

Victor