我正在使用 Telerik radGrid 来显示数据,包括地址。我希望能够插入新地址或编辑现有地址。为此,我希望使用级联组合框来预填充下一个,即 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