使用级联组合框

时间:2012-10-16 19:57:18

标签: telerik radgrid radcombobox

我正在使用Telerik radGrid来显示数据,包括地址。我希望能够插入新的或编辑现有地址。为此,我希望使用Cascading组合框,预先填充下一个,即country_onselectedindexchanged填充省/州等。

我的问题是每当我点击Country时,我都可以在逐步调查中看到我的省/州组合被填充,但随后发生了回发并且我的Grid_itemdatabound事件被触发并且初始数据再次被重新填充。

我在Telerik网站上有一个帐户,但上次我发布了一个问题需要一周的时间才能收到回复。

<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

1 个答案:

答案 0 :(得分:1)

在此发布此消息是因为我不相信我有足够的代表发表评论。通常当我遇到这个问题时,这是因为我在Page_Load中的RadComboBox上有一些绑定事件没有包含在If Not IsPostBack中。尝试这样的事情:

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

希望这有帮助。