Telerik VB Dynamic Data Field Templates - Foreign Key Edit RadComboBox

Here is a Telerik VB Dynamic Data Field Templates - Foreign Key Edit RadComboBox:

foreignKey_edit.ascx

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<telerik:RadComboBox ID="DropDownList1" runat="server">
</telerik:RadComboBox>

 foreignkey_edit.ascx.vb

Imports System.Web.DynamicData
Imports Telerik.Web.UI

Partial Class ForeignKey_EditField
    Inherits System.Web.DynamicData.FieldTemplateUserControl


    Public Overrides ReadOnly Property DataControl() As Control
        Get
            Return DropDownList1
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If (DropDownList1.Items.Count = 0) Then

            Dim dropDownList As New DropDownList()
            If Not Column.IsRequired Then
                dropDownList.Items.Add(New ListItem("[Not Set]", ""))
            End If

            PopulateListControl(dropDownList)

            For Each listItem As ListItem In dropDownList.Items
                Dim comboBoxItem As New RadComboBoxItem()
                comboBoxItem.Text = listItem.Text
                comboBoxItem.Value = listItem.Value
                DropDownList1.Items.Add(comboBoxItem)
            Next
        End If
    End Sub


    Public Function SelectItemByValue(ByVal value As String) As Boolean
        Dim item As RadComboBoxItem
        item = DropDownList1.FindItemByValue(value)
        If item IsNot Nothing Then
            item.Selected = True
            DropDownList1.SelectedValue = value
            DropDownList1.Text = item.Text
            Return True
        Else
            Return False
        End If
    End Function

    Protected Overrides Sub OnDataBinding(ByVal e As EventArgs)
        MyBase.OnDataBinding(e)
        If Mode = DataBoundControlMode.Edit Then
            SelectItemByValue(ForeignKeyColumn.GetForeignKeyString(Row))
        End If
    End Sub

    Protected Overrides Sub ExtractValues(ByVal dictionary As IOrderedDictionary)
        ' If it's an empty string, change it to null
        Dim val As String = DropDownList1.SelectedValue
        If (val = String.Empty) Then
            val = Nothing
        End If
        ExtractForeignKey(dictionary, val)
    End Sub


End Class

For a C# version of Telerik field templates, see Atanas Korchev's blog, Yet another update of RadControls for ASP.NET Ajax DynamicData support.

No Comments