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

Cascading ComboBoxes causes radGrid to reBind

1 Answer 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 16 Oct 2012, 03:35 PM
I'm having a logic issue.  I have some Comboboxes that I need to cascade while in Edit or Insert Mode.  The problem is when I Bind the second Combo based on the first, the entire Grid does a PostBack and so the Combos rebind again.

I know i'm likely just missing an 'If Postback' or something, but my brain is mush right now.

 

<telerik:RadGrid ID="RecipientsGrid" runat="server" AutoGenerateColumns="false" EnableViewState="true" PageSize="5" AllowFilteringByColumn="true" AllowPaging="true" AllowSorting="True">

<EditFormSettings EditFormType="Template">

<FormTemplate>

<telerik:RadComboBox ID="CountryCombo" runat="server">

 

</telerik:RadComboBox>

<telerik:RadComboBox ID="ProvinceCombo" runat="server" Width="325" >

</telerik:RadComboBox>

</FormTemplate>

</EditFormSettings>

</telerik:RadGrid>


    Private Sub RecipientsGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RecipientsGrid.ItemDataBound
 
        If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
            Dim ProvinceCombo As RadComboBox = TryCast(editItem.FindControl("ProvinceCombo"), RadComboBox)
            Dim CountryCombo As RadComboBox = TryCast(editItem.FindControl("CountryCombo"), RadComboBox)
            Dim CityCombo As RadComboBox = TryCast(editItem.FindControl("CityCombo"), RadComboBox)
            '** Populate ComboBoxes and define their Default Value
            Using context As New DataEntities
                With CountryCombo
                    .DataValueField = "CountryId"
                    .DataTextField = "CountryName"
                    .DataSource = context.Countries.OrderBy(Function(x) x.displayOrder).ToList
                End With
                CountryCombo.Width = Unit.Pixel(320)
                CountryCombo.DataBind()
            End Using
 
            Using context As New DataEntities
                With ProvinceCombo
                    .DataValueField = "ProvinceId"
                    .DataTextField = "NameEnglish"
                    .DataSource = context.Provinces.Where(Function(x) x.CountryId = CountryId).OrderBy(Function(x) x.NameEnglish).ToList
 
                End With
                ProvinceCombo.Width = Unit.Pixel(320)
                ProvinceCombo.DataBind()
            End Using
 
 
            Using context As New DataEntities
                With CityCombo
                    .DataValueField = "CityId"
                    .DataTextField = "CityName"
                    .DataSource = context.Cities.Where(Function(x) x.ProvinceID = ProvinceId).OrderBy(Function(x) x.CityName).ToList
                End With
                CityCombo.Width = Unit.Pixel(320)
                CityCombo.DataBind()
            End Using
            SetComboBoxDefault(CountryId, CountryCombo, "Country")
            SetComboBoxDefault(ProvinceId, ProvinceCombo, "Province/State")
            SetComboBoxDefault(CityId, CityCombo, "City")
 
 
        End If
 
 
    End Sub
 
    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)
            ComboBoxItem.Selected = True
 
        Else
            Control.Items.Insert(0, New RadComboBoxItem("-- Please select a " & DisplayText & " --", String.Empty))
        End If
 
    End Sub
 
    Private Sub RecipientsGrid_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RecipientsGrid.ItemCreated
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
 
            'if the item is in edit mode
            Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
            Dim CountryCombo As RadComboBox = DirectCast(editItem.FindControl("CountryCombo"), RadComboBox)
            CountryCombo.AutoPostBack = True
 
            AddHandler CountryCombo.SelectedIndexChanged, AddressOf CountryCombo_SelectedIndexChanged
 
        End If
 
 
    End Sub
 
    Protected Sub CountryCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
 
        If RecipientsGrid.MasterTableView.IsItemInserted <> False Then
 
            Dim insertItem As GridEditFormInsertItem = DirectCast(TryCast(sender, DropDownList).NamingContainer, GridEditFormInsertItem)
 
            Dim ddlList As DropDownList = DirectCast(insertItem.FindControl("Dropdownlist2"), DropDownList)
        Else
 
            Dim CountryCombo As RadComboBox = DirectCast(sender, RadComboBox)
            Dim editedItem As GridEditableItem = DirectCast(TryCast(sender, RadComboBox).NamingContainer, GridEditableItem)
            Dim ProvinceCombo As RadComboBox = DirectCast(editedItem.FindControl("ProvinceCombo"), RadComboBox)
 
            Using context As New DataEntities
                With ProvinceCombo
                    .DataValueField = "ProvinceId"
                    .DataTextField = "NameEnglish"
                    .DataSource = context.Provinces.Where(Function(x) x.CountryId = e.Value).OrderBy(Function(x) x.NameEnglish).ToList
                End With
                ProvinceCombo.Width = Unit.Pixel(320)
                ProvinceCombo.DataBind()
            End Using
 
        End If
 
    End Sub





1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 22 Oct 2012, 09:02 AM
Hello Tim,

I could not see anything suspicious in your code - furthermore, you have put the data-binding code for the combos in the ItemDataBound event and that's executed only when the grid rebinds/databinds. I suspect that you are calling the grid's DataBind() method in the Page_Load event or at least are using Simple Databinding as opposed to Advanced data-binding. Do rework your code along the lines of the latter approach and see the result.


Greetings, Tsvetoslav
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
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or