This is a migrated thread and some comments may be shown as answers.

Setting SelectedValue not working

2 Answers 859 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 19 Dec 2019, 06:04 AM

I have a user control in VB.Net which creates a dynamic RadDropDownList at runtime.  The create code is as follows

    Private Sub SetControl()
        Try
                        Dim list As New RadDropDownList
                        list.Font = mFont
                        list.Location = New Point(213, 0)
                        list.Size = New Size(225, 25)
                        LoadList(list)
                        list.Name = "userCTRL"
                        Me.Controls.Add(list)
        Catch ex As Exception
            Dim x As String = ex.Message
        End Try
    End Sub

    Private Sub LoadList(ByRef lst As RadDropDownList)

        Dim strSQL As String = "SELECT AssetTypeAttributeListValueId, Label FROM dbo.AssetTypeAttributeListValue WHERE AssetTypeAttributeId = " & AttributeTypeId & " ORDER BY SortOrder"
        Dim ds As DataSet = CLIB_MAPPS.Configuration.DBAccess.getDataSet(strSQL, "Attribute")
        If ds.Tables(0).Rows.Count > 0 Then
            lst.ValueMember = "AssetTypeAttributeListValueId"
            lst.DisplayMember = "Label"
            lst.DataSource = ds.Tables(0)
        End If
    End Sub

 

Another Public property of the user control sets the value of the control from the form that contains this user control.  Here is the public property of the user control:

    Public Property CollectedValue() As String
        Get
            GetValue()
            Return mCollectedValue
        End Get
        Set(ByVal value As String)
            mCollectedValue = value
            SetValue()
        End Set
    End Property

When the value is set it calls the SetValue() routine to find the RadDropDownList control and set its value

    Private Sub SetValue()
        Try
                    Dim list As RadDropDownList = CType(Me.Controls("userCTRL"), RadDropDownList)
                    If mCollectedValue = "" Then
                        list.SelectedIndex = -1
                    Else
                        list.SelectedValue = CInt(mCollectedValue)
                    End If
        Catch ex As Exception
            Dim x As String = ex.Message
        End Try
    End Sub

After I create a local instance of the RadDropDownList called "list", I attempt to set the SelectedValue, and it will not change.  It is acting almost like the control doesn't exist, but doesn't throw any errors.  In the Debug mode, I can see that SelectedIndex is -1 and SelectedValue is Nothing, and is of type "Object".  If I view the value of list in the Quick Watch, it clearly shows it set as a control of type RadDropDownList, and it contains the list items I added when the control was created.  Also, if I stop it in Debug on the IF statement in SetValue() and expand the variable called "list" to see all its properties, then immediately close the Debug window, it recognizes the SelectedIndex and SelectedValue and it sets them properly.  Once I do that, the SelectedValue type also immediately changes to "Object(Integer)".

I'm not sure how to adequately explain this, but everything I have tried to get it to recognize the properties of "list" does not seem to work.  Clearly stopping the code, opening in the Quick Watch in Debug and expanding the "list" variable to see the properties does something to allow the "list" variable to be fully loaded and its properties available.  I tried Application.DoEvents(), and other events after creating "list", but nothing seems to work.

Thoughts?

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Dec 2019, 11:34 AM
Hello, Mark,     

Following the provided sample code snippet, I have prepared a sample project to test the behavior. I was able to observe the described problem when setting the SelectedValue property of RadDropDownList.

I have noticed that you set the DataSource property before adding the RadDropDownList in the Controls collection and before initializing its BindingContext. That is why in order to get the desired value selected when you set the UserControl11.CollectedValue property, it is necessary to initialize the RadDropDownList.BindingContext property before setting its DataSource.

I have attached my sample project for your reference.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mark
Top achievements
Rank 1
answered on 19 Dec 2019, 03:13 PM

Dess,

This worked perfectly. 

I'm glad you understood my ramblings and was able to recreate my issue.   I knew I was missing something in creating this control dynamically, but couldn't figure it out. 

Thanks again for your quick response and assistance!

Tags
DropDownList
Asked by
Mark
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mark
Top achievements
Rank 1
Share this question
or