سؤال

I'm using a Telerik radGrid to display data, including address. I want to be able to insert new, or Edit an existing address. To this end, I wish to use Cascading comboboxes which would prepopulate the next, ie country_onselectedindexchanged populates Province/State, etc.

My issue is that whenever I click on Country, I can see in the step-through that my Province/State combo is populated, but then a postback occurs and my Grid_itemdatabound event fires and the initial data is repopulated again.

I have an account on the Telerik site, but last time I posted a question it took a week to get a response.

<telerik:RadGrid ID="RecipientsGrid" runat="server" AutoGenerateColumns="false"  EnableViewState="true" PageSize="5" 
                    AllowFilteringByColumn="true" AllowPaging="true" AllowSorting="True">
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
            <MasterTableView CommandItemDisplay="Bottom" DataKeyNames="RecipientOrganizationID" EditMode="EditForms" >   
                <EditFormSettings EditFormType="Template">
                <FormTemplate>
                                    <telerik:RadComboBox ID="CountryCombo" runat="server" DataTextField="CountryName" DataValueField="CountryID"  
                                                       OnSelectedIndexChanged="CountryCombo_SelectedIndexChanged" AutoPostBack="true">
                                    </telerik:RadComboBox>
                                    <telerik:RadComboBox ID="ProvinceCombo" runat="server" Width="325" EnableLoadonDemand="true" OnSelectedIndexChanged="ProvinceCombo_SelectedIndexChanged" AutoPostBack="true" >
                                    </telerik:RadComboBox>
                </FormTemplate>
            </EditFormSettings>

            </MasterTableView>

        </telerik:RadGrid>

Private Sub RecipientsGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RecipientsGrid.NeedDataSource

                Dim ctx As New DataEntities
                RecipientsGrid.DataSource = ctx.RecipientOrganizations.ToList

                AddOrganizationButton.Visible = False
                RecipientOrganizationComboBox.Visible = False

    End Sub

 Private Sub RecipientsGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RecipientsGrid.ItemDataBound

        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)

            If e.Item.OwnerTableView.IsItemInserted Then

                Dim CountryCombo As RadComboBox = TryCast(editedItem.FindControl("CountryCombo"), RadComboBox)
                Dim ProvinceCombo As RadComboBox = TryCast(editedItem.FindControl("ProvinceCombo"), RadComboBox)
                Dim CityCombo As RadComboBox = TryCast(editedItem.FindControl("CityCombo"), RadComboBox)

                LoadCountries(CountryCombo)

            Else

            End If

        End If

    End Sub

    Protected Sub CountryCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)

        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)

        LoadProvinces(e.Value, ProvinceCombo)

    End Sub

    Protected Sub ProvinceCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)

        Dim ProvinceCombo As RadComboBox = DirectCast(sender, RadComboBox)
        Dim editedItem As GridEditableItem = DirectCast(TryCast(sender, RadComboBox).NamingContainer, GridEditableItem)
        Dim CityCombo As RadComboBox = DirectCast(editedItem.FindControl("CityCombo"), RadComboBox)

        LoadCities(e.Value, CityCombo)

    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.displayOrder).ToList
            End With
            Control.Width = Unit.Pixel(320)
            Control.DataBind()
        End Using

    End Sub

    Protected Sub LoadProvinces(ByVal countryID As Integer, ByVal Control As RadComboBox)

        Using context As New DataEntities
            With Control
                .DataValueField = "ProvinceId"
                .DataTextField = "NameEnglish"
                .DataSource = context.Provinces.Where(Function(x) x.CountryId = countryID).OrderBy(Function(x) x.NameEnglish).ToList
            End With
            Control.Width = Unit.Pixel(320)
            Control.DataBind()
        End Using

    End Sub

    Protected Sub LoadCities(ByVal ProvinceId As Integer, ByVal Control As RadComboBox)

        Using context As New DataEntities
            With Control
                .DataValueField = "CityId"
                .DataTextField = "CityName"
                .DataSource = context.Cities.Where(Function(x) x.ProvinceID = ProvinceId).OrderBy(Function(x) x.CityName).ToList
            End With
            Control.Width = Unit.Pixel(320)
            Control.DataBind()
        End Using

    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)
            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
هل كانت مفيدة؟

المحلول

Posting this here because I don't believe I have enough rep to comment. Usually when I have this issue, it's because I have a binding event of some sort on my RadComboBox in Page_Load that isn't wrapped in a If Not IsPostBack. Try something like this:

Sub Page_Load
    If Not IsPostBack
        RadComboBox.DataSource = foo;
        RadComboBox.DataBind();
    End If
End Sub

Hope this helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top