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

Cannot seem to set a Default Value to combobox

1 Answer 52 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 18 Oct 2012, 07:12 PM
The below code works on another page but the CountryCombo always says Afghanistan (alpha order) when in InsertMode.  This is a radWindow page, hence the querystrings at the beginning.  I've attached everything so the page lifecycle is clear.  For some reason ItemCreated always fires twice, and when I step though, I see it Select the correct record, but when the form displays, it always says Afghanistan.

  Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
 
        recipientOrganizationID = Request.QueryString.Get("ID")
        DisplyMode = Request.QueryString.Get("show")
 
        If recipientOrganizationID = Nothing AndAlso DisplyMode Is Nothing Then
            RecipientOrganizationView.ShowInsertItem()
        Else
            For i = 0 To RecipientOrganizationView.PageSize - 1
                RecipientOrganizationView.EditIndexes.Add(i)
            Next
        End If
 
    End Sub
 
    Private Sub RecipientOrganizationView_ItemCreated(sender As Object, e As Telerik.Web.UI.RadListViewItemEventArgs) Handles RecipientOrganizationView.ItemCreated
 
        If TypeOf e.Item Is RadListViewEditableItem AndAlso e.Item.IsInEditMode Then
 
            'if the item is in edit mode
            Dim editItem As RadListViewEditableItem = DirectCast(e.Item, RadListViewEditableItem)
            Dim CountryCombo As RadComboBox = DirectCast(editItem.FindControl("CountryCombo"), RadComboBox)
            CountryCombo.AutoPostBack = True
 
            Dim ProvinceCombo As RadComboBox = DirectCast(editItem.FindControl("ProvinceCombo"), RadComboBox)
            ProvinceCombo.AutoPostBack = True
 
            AddHandler CountryCombo.SelectedIndexChanged, AddressOf CountryCombo_SelectedIndexChanged
            AddHandler ProvinceCombo.SelectedIndexChanged, AddressOf ProvinceCombo_SelectedIndexChanged
 
        End If
 
        If TypeOf e.Item Is RadListViewInsertItem Then
 
            Dim editItem As RadListViewEditableItem = DirectCast(e.Item, RadListViewEditableItem)
            Dim CountryCombo As RadComboBox = DirectCast(editItem.FindControl("CountryCombo"), RadComboBox)
 
            LoadCountries(CountryCombo)
            SetComboBoxDefault(43, CountryCombo, "Country")
 
            'Dim ComboBoxItem As RadComboBoxItem = CountryCombo.FindItemByValue(43)
            'ComboBoxItem.Selected = True
 
        End If
 
    End Sub
 Private Sub RecipientOrganizationView_ItemDataBound(sender As Object, e As Telerik.Web.UI.RadListViewItemEventArgs) Handles RecipientOrganizationView.ItemDataBound
 
        If TypeOf e.Item Is RadListViewEditableItem AndAlso e.Item.IsInEditMode Then
 
            Dim item As RadListViewDataItem = TryCast(e.Item, RadListViewDataItem)
            Dim CountryId As String = CType(DataBinder.Eval(item.DataItem, "CountryId"), String)
            Dim ProvinceId As String = CType(DataBinder.Eval(item.DataItem, "ProvinceId"), String)
            Dim CityId As String = CType(DataBinder.Eval(item.DataItem, "CityId"), String)
 
            Dim CountryCombo As RadComboBox = CType(e.Item.FindControl("CountryCombo"), RadComboBox)
            LoadCountries(CountryCombo)
            If CountryId IsNot Nothing Then
                Dim ComboBoxItem As RadComboBoxItem = CountryCombo.FindItemByValue(CountryId)
                ComboBoxItem.Selected = True
            End If
 
            Dim ProvinceCombo As RadComboBox = CType(e.Item.FindControl("ProvinceCombo"), RadComboBox)
            LoadProvinces(CountryId, ProvinceCombo)
            If ProvinceId IsNot Nothing Then
                Dim ComboBoxItem As RadComboBoxItem = ProvinceCombo.FindItemByValue(ProvinceId)
                ComboBoxItem.Selected = True
            End If
 
            Dim CityCombo As RadComboBox = CType(e.Item.FindControl("CityCombo"), RadComboBox)
            LoadCities(ProvinceId, CityCombo)
            If CityId IsNot Nothing Then
                Dim ComboBoxItem As RadComboBoxItem = CityCombo.FindItemByValue(CityId)
                ComboBoxItem.Selected = True
            End If
 
        End If
 
 
    End Sub
    Private Sub RecipientOrganizationView_NeedDataSource(sender As Object, e As Telerik.Web.UI.RadListViewNeedDataSourceEventArgs) Handles RecipientOrganizationView.NeedDataSource
 
        Dim repository As New DataEntities
        RecipientOrganizationView.DataSource = repository.RecipientOrganizations.Where(Function(x) x.RecipientOrganizationID = recipientOrganizationID).ToList
 
    End Sub
 
Public Sub CountryCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
 
        Dim btn = CType(sender, RadComboBox)
        Dim item = CType(btn.NamingContainer, RadListViewEditableItem)
        Dim Combo As RadComboBox = CType(item.FindControl("ProvinceCombo"), RadComboBox)
 
        Combo.ClearSelection()
        LoadProvinces(e.Value, Combo)
 
    End Sub
Protected Sub LoadCountries(ByVal Control As RadComboBox)
 
        Using context As New DataEntities
            With Control
                .DataValueField = "CountryId"
                .DataTextField = "CountryName"
                .DataSource = context.Countries.OrderBy(Function(x) x.CountryName).ToList
            End With
            Control.Width = Unit.Pixel(225)
            Control.DataBind()
        End Using
 
    End Sub
   ''' <summary>
    ''' Checks to see of the Provice/State or Country combox should have Preselected data, otherwise Default Data is Presented
    ''' </summary>
    ''' <param name="FindItemByValue"></param>
    ''' <param name="Control"></param>
    ''' <param name="DisplayText"></param>
    Public Sub SetComboBoxDefault(ByVal FindItemByValue As Integer, ByVal Control As RadComboBox, ByVal DisplayText As String)
 
        Dim ComboBoxItem As RadComboBoxItem
 
        If FindItemByValue > 0 Then
            ComboBoxItem = Control.FindItemByValue(FindItemByValue)
            If ComboBoxItem IsNot Nothing Then
                ComboBoxItem.Selected = True
            Else
                Control.Items.Insert(0, New RadComboBoxItem("-- Please select a " & DisplayText & " --", String.Empty))
            End If
        Else
            Control.Items.Insert(0, New RadComboBoxItem("-- Please select a " & DisplayText & " --", String.Empty))
        End If
 
    End Sub




1 Answer, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 23 Oct 2012, 01:42 PM
Hi Tim,

Unfortunately, the provided snippet of code is not sufficient to determine what might cause the problematic behavior. Could you provide us a runnable sample which demonstrates the problem? In addition, I could suggest you to check if you register the ItemCreated event in the markup as you do in the code behind, because this could cause the event firing twice.


All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
ComboBox
Asked by
Tim
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Share this question
or