Passing multiple select radcombox as control parameters for the objectdatasource

2 posts, 0 answers
  1. Quynh
    Quynh avatar
    12 posts
    Member since:
    Sep 2011

    Posted 25 Jun 2014 Link to this post

    Requirements

    Telerik Product and Version


    Supported Browsers and Platforms


    Components/Widgets used (JS frameworks, etc.)



    PROJECT DESCRIPTION 
    [enter description here, together with step-by-step instructions on how to use the project]
    Hi, How do I pass the checked values of the radcombobox (with checkbox option) to an object datasource? I know I can get all the checked values then put to a session variable then use session parameter. But is there any easier way?
  2. Gidon
    Gidon avatar
    7 posts
    Member since:
    Apr 2014

    Posted 29 Aug Link to this post

    You could write a custom class inheriting from System.Web.UI.WebControls.Parameter that parses the RadComboBox and passes the data in a way that your back end understands, but since you're technically passing more than one value this can get tricky when you involve user types and table valued parameters.

    If you know the RadComboBoxItems ahead of time you can use a custom Parameter class to get a single true/false value fairly easily:

    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    using Telerik.Web.UI;
     
    public class RadComboBoxItemParameter : Parameter {
      [IDReferenceProperty, TypeConverter(typeof(ControlIDConverter))]
      public String ControlID {
        get {
          return (String)ViewState["ControlID"] ?? String.Empty;
        }
        set {
          ViewState["ControlID"] = value;
        }
      }
     
      public String ItemText {
        get {
          return (String)ViewState["ItemText"] ?? String.Empty;
        }
        set {
          ViewState["ItemText"] = value;
        }
      }
     
      public String ItemValue {
        get {
          return (String)ViewState["ItemValue"] ?? String.Empty;
        }
        set {
          ViewState["ItemValue"] = value;
        }
      }
     
      public static Control FindControlRecursive(Control control, String id) {
        Control foundControl = control.FindControl(id);
        if (foundControl != null) {
          return foundControl;
        }
     
        foreach (Control child in control.Controls) {
          foundControl = FindControlRecursive(child, id);
          if (foundControl != null) {
            return foundControl;
          }
        }
     
        return null;
      }
     
      protected override Object Evaluate(HttpContext httpContext, Control control) {
        if (control == null) {
          return null;
        }
     
        if (String.IsNullOrEmpty(ControlID)) {
          throw new ArgumentException("ControlID must be specified.", ControlID);
        }
        if (!String.IsNullOrEmpty(ItemText) && !String.IsNullOrEmpty(ItemValue)) {
          throw new InvalidOperationException("Only one of ItemText or ItemValue may be specified.");
        }
        if (String.IsNullOrEmpty(ItemText) && String.IsNullOrEmpty(ItemValue)) {
          throw new InvalidOperationException("One of ItemText or ItemValue must be specified.");
        }
     
        Control foundControl = FindControlRecursive(control.Page, ControlID);
        if (foundControl == null) {
          throw new InvalidOperationException("Control not found.");
        }
     
        RadComboBox radComboBox = foundControl as RadComboBox;
        if (radComboBox == null) {
          throw new InvalidOperationException("Control must be a RadComboBox.");
        }
     
        RadComboBoxItem radComboBoxItem = null;
        if (!String.IsNullOrEmpty(ItemText)) {
          radComboBoxItem = radComboBox.FindItemByText(ItemText);
        } else if (!String.IsNullOrEmpty(ItemValue)) {
          radComboBoxItem = radComboBox.FindItemByValue(ItemValue);
        }
     
        if (radComboBoxItem == null) {
          return null;
        }
     
        return radComboBoxItem.Checked;
      }
    }

    Usage:

    <telerik:RadComboBox runat="server" CheckBoxes="True" ID="radComboBox">
      <Items>
        <telerik:RadComboBoxItem runat="server" Text="Yes" Value="True" />
        <telerik:RadComboBoxItem runat="server" Text="No" Value="False" />
      </Items>
    </telerik:RadComboBox>
    <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:DbConnectionString %>" ID="sqlDataSource" runat="server" SelectCommand="RadComboBox_SELECT" SelectCommandType="StoredProcedure">
      <SelectParameters>
        <cc:RadComboBoxItemParameter ControlID="radCombBox" ItemText="Yes" Name="IncludeYes" />
        <cc:RadComboBoxItemParameter ControlID="radCombBox" ItemValue="False" Name="IncludeNo" />
      </SelectParameters>
    </asp:SqlDataSource>

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top