How to bind a hash to RadFilterCustomEditors.RadFilterDropDownEditor for choices.

3 posts, 0 answers
  1. Matthew
    Matthew avatar
    10 posts
    Member since:
    Oct 2008

    Posted 10 Jan 2011 Link to this post

    I am using a Filter control that is never bound to a grid or list view.  I have a set of fields that are passed into my user control, and I iterate over those fields to build the FieldEditor controls that I need in my Filter control.  This works well for all the standard FieldEditor types, (ie. boolean, numeric, text) However, I am having trouble populating the choices for editors of type RadFilterCustomEditors.RadFilterDropDownEditor  I am using the following code called from my user controls pre render event.

    Dim choices As Dictionary(Of String, String) = New Dictionary(Of String, String)()
    choices.Add(
    "Yes", "Yes")
    choices.Add(
    "No", "No")

    Dim field As RadFilterCustomEditors.RadFilterDropDownEditor = New RadFilterCustomEditors.RadFilterDropDownEditor()
    field.DataSourceID = "????" 'What would go here?
    field.DataValueField =
    "Key"                       
    field.DataTextField =
    "Value"

    QuestionFilter.FieldEditors.Add(field)
    field.FieldName = question.Id                       
    field.DisplayName = question.Description

    obviously this is incomplete and doesn't work.  I have tried creating a FilterChoice object with a display and value property and binding a list of those to an objectdatasource, but ran into issues there with strings being in the wrong format, and that really isn't the best implementation anyway, so I have abandoned it for the time being.

    So my question is in a nutshell, how do I build a list of choices, and bind it to a RadFilterCustomEditors.RadFilterDropDownEditor at run time without being tied to a grid?
  2. Matthew
    Matthew avatar
    10 posts
    Member since:
    Oct 2008

    Posted 10 Jan 2011 Link to this post

    This is my latest implementation using an ObjectDataSource. Here is the source for my user control

    ASPX
    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="QuestionFilter.ascx.vb" Inherits="Reporting_QuestionReport_UserControls_QuestionFilter" %>
    <asp:Panel id="QuestionFilterPanel" runat="server">
        <telerik:RadFilter ID="QuestionFilter" runat="server" ShowApplyButton="false" ShowLineImages="true"/></asp:Panel>

    Code Behind
    Imports Telerik.Web.UI

    Namespace Reporting.QuestionReport.UserControls
    Public Class FilterChoice
    Private _displayText As String
    Public Property DisplayText() As String
    Get
    Return _displayText
    End Get
    Set(ByVal value As String)
    _displayText = value
    End Set
    End Property

    Private _valueText As String
    Public Property ValueText() As String
    Get
    Return _valueText
    End Get           
    Set(ByVal value As String)
    _valueText = value
    End Set
    End Property

    Public Sub New(ByVal value As String, ByVal display As String)
    _displayText = display
    _valueText = value
    End Sub

    Public Function GetYesNoFilterChoiceCollection() As IEnumerable(Of FilterChoice)
    Dim list As List(Of FilterChoice) = New List(Of FilterChoice)
    list.Add(
    New FilterChoice("Yes", "Yes"))
    list.Add(
    New FilterChoice("No", "No"))
    Return list
    End Function
    End Class
    End Namespace

    Partial
    Class Reporting_QuestionReport_UserControls_QuestionFilter
    Inherits ReportParameterSelectorUserControl

    Private Const QuestionType_YesNo As String = "Yes/No"
    Private Const QuestionType_Number As String = "Number"
    Private Const QuestionType_Date As String = "Date"
    Private Const QuestionType_SingleAnswer As String = "Single Answer"
    Private Const QuestionType_MultiAnswer As String = "Multi Answer"
    Private Const QuestionType_AggregateCompliance As String = "Aggregate Compliance"

    Public Property AvailableQuestions() As QuestionCollection
    Get
    Return CType(ViewState("AvailableQuestions"), QuestionCollection)
    End Get
    Set(ByVal value As QuestionCollection)
    ViewState(
    "AvailableQuestions") = value
    End Set
    End Property

    Private _yesNoDatasource As ObjectDataSource

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    _yesNoDatasource =
    New ObjectDataSource()
    _yesNoDatasource.ID =
    "YesNoChoices"
    _yesNoDatasource.TypeName =
    "Reporting.QuestionReport.UserControls.FilterChoice"
    _yesNoDatasource.SelectMethod =
    "GetYesNoFilterChoiceCollection"

    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    AddQuestionsToQuestionFilter()

    End Sub

    Private Sub AddQuestionsToQuestionFilter()

    If Not AvailableQuestions Is Nothing Then

    QuestionFilter.ExpressionPreviewPosition = RadFilterExpressionPreviewPosition.Bottom
    QuestionFilter.FieldEditors.Clear()
    For Each question In AvailableQuestions
    Select Case question.QuestionType.Description
    Case QuestionType_YesNo

    Dim field As RadFilterCustomEditors.RadFilterDropDownEditor = New RadFilterCustomEditors.RadFilterDropDownEditor()
    field.DataSourceID =
    "YesNoChoices"
    field.DataValueField = "DisplayText"
    field.DataTextField = "ValueText"

    QuestionFilter.FieldEditors.Add(field)
    field.FieldName = question.Id
    field.DisplayName = question.Description

    Case QuestionType_Number
    Dim field As RadFilterNumericFieldEditor = New RadFilterNumericFieldEditor()

    QuestionFilter.FieldEditors.Add(field)
    field.FieldName = question.Id
    field.DisplayName = question.Description

    Case QuestionType_Date
    Dim field As RadFilterDateFieldEditor = New RadFilterDateFieldEditor()

    QuestionFilter.FieldEditors.Add(field)
    field.FieldName = question.Id
    field.DisplayName = question.Description

    'Case QuestionType_SingleAnswer
    '    field = New RadFilterCustomEditors.RadFilterDropDownEditor()
    'Case QuestionType_MultiAnswer
    '    field = New RadFilterCustomEditors.RadFilterDropDownEditor()
    'Case QuestionType_AggregateCompliance
    End Select
    Next
    End If
    End Sub
    End Class
    I'm recieving the following error when trying to add a filter expression to the filter control at run time

    FieldEditors collection is empty.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: FieldEditors collection is empty.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceException: FieldEditors collection is empty.]
    Telerik.Web.UI.RadFilterDataFieldEditorCollection.System.Collections.Generic.IList<Telerik.Web.UI.RadFilterDataFieldEditor>.get_Item(Int32 index) +116
    Telerik.Web.UI.RadFilter.AddChildExpression(RadFilterGroupExpressionItem groupItem, Boolean isGroup) +83
    Telerik.Web.UI.RadFilterCommandEventArgs.ExecuteCommand(Object source) +134
    Telerik.Web.UI.RadFilter.OnBubbleEvent(Object source, EventArgs args) +139
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
    Telerik.Web.UI.RadFilterExpressionItem.OnBubbleEvent(Object source, EventArgs args) +77
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
    System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
    System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
    System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

    If I don't have any drop down field editors the control works just fine.  I think this has to do with the page life cycle, and when I am setting up the field editors, however, since I have to recieve the available fields from the Parent page, and the property is set after page load fires I think I have to use the PreRender event. 

    I'm at a loss, any help you can provide is appreciated.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Matthew
    Matthew avatar
    10 posts
    Member since:
    Oct 2008

    Posted 11 Jan 2011 Link to this post

    solved by implementing the RadFilterDropDownFieldEditor found here:

    http://www.telerik.com/community/forums/aspnet-ajax/filter/error-when-building-custom-radfilterdatafieldeditor.aspx
Back to Top