- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Databinding with RadComboBox
Joined: 17-Oct-2008
Hi
I want to bind a RadComboBox to the llblgendatasource2 control. I have just started with llblgen and web development so bear with me. I am looking to create an AJAX based CRUD template using llblgen template studio, within which I am using telerik rad controls and dev express grid. Now to the point at hand, for the sake of simplifying I have a order table and a product table . When inserting or editing an order users will select a product by name (not ID) from a radcomboox using their built in on demand mechanism (On Demand mechanism - you can set a poll-server timeout and the combobox fires a server-side event (ItemsRequested) that returns all the relative matches for the currently typed text). I am using on demand to reduce load time and unnecessary fetching of all the rows in the table which contains a vast amount of records. Now the following code is fine when not enclosed within a asp:FormView but when it is I receive the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Below is an Aspx snippet you may notice at present i’m working with an altered version of the ASP.NET 2.0 GUI generator templates).
<asp:FormView ID="frmEditOrders" DataKeyNames="OrderId" runat="server"
DefaultMode="insert" DataSourceID="_OrdersDS" Cellpadding="0"
OnItemCommand="frmEditOrders_ItemCommand"
OnItemCreated="frmEditOrders_ItemCreated">
<EditItemTemplate>
<table cellpadding="3" border="0" cellspacing="1" class="formtexts">
<tr>
<td width="20" nowrap="nowrap" class="content"> </td>
<td class="formtexts" >
ProductId:
</td>
<td class="content">
<llblgenpro:LLBLGenProDataSource2 ID="_ProductsDS" runat="server"
DataContainerType="EntityCollection"
AdapterTypeName="SevenLayers.AdapterDataLayer.DatabaseSpecific.DataAccessAdapter, SevenLayers.AdapterDataLayerDBSpecific" EntityFactoryTypeName="SevenLayers.AdapterDataLayer.FactoryClasses.ProductsEntityFactory, SevenLayers.AdapterDataLayer"
LivePersistence="False" onperformselect="_ProductsDS_PerformSelect"
MaxNumberOfItemsToReturn="10" >
<SelectParameters>
<asp:ControlParameter ControlID="ddlProductId" Name="filterParameter"
PropertyName="SelectedValue" />
</SelectParameters>
</llblgenpro:LLBLGenProDataSource2>
<telerik:RadComboBox ID="ddlProductId" Runat="server" SelectedValue='<%# Bind("ProductId") %>'
DataSourceID="_ProductsDS" DataTextField="Name" DataValueField="ProductId"
EnableLoadOnDemand="True" EnableVirtualScrolling="True"
onitemsrequested="ddlProductId_ItemsRequested" ShowMoreResultsBox="true">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</telerik:RadComboBox>
</td>
<td width="20" nowrap="nowrap" class="content"> </td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
And sample code behind file:
IRelationPredicateBucket filter = new RelationPredicateBucket();
protected void _ProductsDS_PerformSelect(object sender, PerformSelectEventArgs2 e)
{
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(e.ContainedCollection, filter, e.MaxNumberOfItemsToReturn, e.Sorter, e.PrefetchPath, e.PageNumber, e.PageSize);
}
}
protected void ddlProductId_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
filter.PredicateExpression.Clear();
RadComboBox combo = (RadComboBox)o;
string queryString = e.Text + "%";
PredicateExpression filterExpression = new PredicateExpression();
filterExpression.AddWithAnd(ProductsFields.Name % queryString);
filter.PredicateExpression.Add(filterExpression);
combo.DataBind();
}
Any help would be greatly appreciated. Thankyou.
V.2.6. .NET 3.5
I think your code should look like:
ASPX
<llblgenpro:LLBLGenProDataSource2 ID="_ProductsDS" runat="server"
DataContainerType="EntityCollection"
AdapterTypeName="SevenLayers.AdapterDataLayer.DatabaseSpecific.DataAccessAdapter,
SevenLayers.AdapterDataLayerDBSpecific"
EntityFactoryTypeName= "SevenLayers.AdapterDataLayer.FactoryClasses.ProductsEntityFactory, SevenLayers.AdapterDataLayer"
LivePersistence="False" onperformselect="_ProductsDS_PerformSelect"
MaxNumberOfItemsToReturn="10" >
</llblgenpro:LLBLGenProDataSource2>
<telerik:RadComboBox
ID="ddlProductId" Runat="server"
SelectedValue="ProductId"
DataSourceID="_ProductsDS"
DataTextField="Name"
DataValueField="ProductId"
EnableLoadOnDemand="True"
EnableVirtualScrolling="True"
onitemsrequested="ddlProductId_ItemsRequested" ShowMoreResultsBox="true">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</telerik:RadComboBox>
Note that you can't use Bind on a combo that is based on a dataSource that doesn't belong to the data context created by the surrounded FormView.
And, I would recommend you to use_ LivePersistence=true _as you are not doing anything special at PerformSelect.
Also, If you don't have many Products, why not only fetch them once. I think the ItemRequested you are using and the way you are binding would push many unnecessary times your server. IMHO you don't need code-behind here.
I also could recommend to you to review the ASP.Net DataBinding example at http://llblgen.com/pages/examples.aspx . That project has an Ajax example using LLBLGenProDataSource.