To convert code
from posted project(s) |
Telerik online converter |
RadControl version
|
RadComboBox v2.x or Telerik.Web.UI v2007.3 1425
|
.NET version |
2.0
|
Visual Studio version |
VS.NET 2005
|
programming language |
C#
|
browser support |
all browsers supported by RadComboBox
|
PROJECT DESCRIPTION
Data binding RadComboBox to a declarative datasource control using its DataSourceID property is an easy task. It is relatively simple to set up dependent combo boxes that request subsets of a dataset using a data source ControlParameter that points to another combo box's SelectedValue property:
<asp:ObjectDataSource ID="citiesDataSource" runat="server" OldValuesParameterFormatString="original_{0}" |
SelectMethod="GetCities" TypeName="ComboDataLayer"> |
<SelectParameters> |
<asp:ControlParameter ControlID="RadComboBox2" DefaultValue="1" Name="countryID" PropertyName="SelectedValue" |
Type="String" /> |
</SelectParameters> |
</asp:ObjectDataSource> |
Unfortunately this is very hard to do when the control has its load on demand feature turned on. RadComboBox sends Ajax requests to the server and keeps them extremely lightweight -- the data sent contains only the control id and the text that has been typed so far. The SelectedValue property is not populated and we cannot use a ControlParameter.
We can work around that by creating a custom data source parameter class and add our data retrieval logic there. This way we can set up our data source like this:
<asp:ObjectDataSource ID="countriesDataSource" runat="server" OldValuesParameterFormatString="original_{0}" |
SelectMethod="GetCountries" TypeName="ComboDataLayer"> |
<SelectParameters> |
<telerik:AjaxComboValueParameter AjaxComboID="RadComboBox2" DefaultValue="1" Name="continentID" |
Type="String" /> |
</SelectParameters> |
</asp:ObjectDataSource> |
Note the AjaxComboID property. We use it to detect if the current Ajax request has been initiated by our target combo box.
We have to set the combo box's DataSourceID and data bind it in the ItemsRequested event handler:
protected void RadComboBox2_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) |
{ |
RadComboBox2.DataSourceID = "countriesDataSource"; |
|
RadComboBox2.DataBind(); |
} |
And finally the AjaxComboValueParameter code:
/// <summary> |
/// An AjaxComboValueParameter instance will return the currently typed value of |
/// the combobox that has initiated the Ajax request. In cases where we are not inside an Ajax request |
/// or the request has been initiated by another combo, we return the default value. |
/// </summary> |
public class AjaxComboValueParameter : Parameter |
{ |
public AjaxComboValueParameter() |
{ |
|
} |
|
/// <summary> |
/// The server ID of the combo that will initiate the Ajax requests. |
/// </summary> |
public string AjaxComboID |
{ |
get |
{ |
return ViewState["AjaxComboID"] as string ?? string.Empty; |
} |
set |
{ |
ViewState["AjaxComboID"] = value; |
} |
} |
|
protected override object Evaluate(HttpContext context, Control control) |
{ |
string ajaxInitiator = context.Request.QueryString["rcbServerID"]; |
if (!string.IsNullOrEmpty(ajaxInitiator)) |
{ |
if (ajaxInitiator.Contains(AjaxComboID)) |
{ |
string comboValue = context.Request.QueryString["text"]; |
return comboValue; |
} |
else |
{ |
return DefaultValue; |
} |
} |
else |
{ |
return base.Evaluate(context, control); |
} |
} |
|
|
} |
PROJECT DESCRIPTION
Same as the above, without the highlighted parts. Here, RadComboBox will be automatically bound to its corresponding data source which is declared in the ASPX.