This is a migrated thread and some comments may be shown as answers.

Passing multiple select radcombox as control parameters for the objectdatasource

1 Answer 273 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Quynh
Top achievements
Rank 1
Quynh asked on 25 Jun 2014, 11:10 PM

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?

1 Answer, 1 is accepted

Sort by
0
Gidon
Top achievements
Rank 1
answered on 29 Aug 2016, 05:14 PM

You could write a custom class inheriting from System.Web.UI.WebControls. 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 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>

Tags
ComboBox
Asked by
Quynh
Top achievements
Rank 1
Answers by
Gidon
Top achievements
Rank 1
Share this question
or