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

RadCombobox With Load On Demand In RadGrid Not Obtaining Selected Value

1 Answer 291 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
jgill
Top achievements
Rank 1
jgill asked on 19 Jun 2010, 11:15 PM
I am having problems with setting the selected value of a RadCombobox that loads items on demand.

I have a RadComboBox as a templated item within a RadGrid and the page I have is geared towards batch updates in a Radgrid and based on this example: http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/clienteditbatchupdates/defaultcs.aspx, except for some very important differences in the type of drop down used which may be contributing to the issues.

Unlike the example, I use a RadCombobox that loads items on demand instead of a regular dropdown and I want to bind the RadCombobox to a nullable item.  I am unable to bind the SelectedValue property of the RadComboBox to the value I want, AccountID, using a simple Bind("AccountID") statement because the value can (and often is) null (which is valid in my scenario).

When I double click a row to put it in "edit mode", e.g. display the Radcombobox and hide the Label, no items are loaded into the combobox until I click on it (due to the load on demand).  I have been unable to get the RadCombobox to have it's selected value initially set to a SelectedValue because of this and I wondering what I am doing wrong/missing.

I can tell that cb.SelectedValue is being set to the correct value in the RadGrid's ItemDatabound event, but at that time there are 0 items loaded into a RadCombobox for a particular row.  I have also tried commenting out ClearSelection in the item load in the RadCombobox's ItemsRequested method, but that has no effect.

One thing I thought is that, since lblAccountID also contains the value I would want to bind/select in the RadCombobox there could be a JavaScript solution to this, where after the items are loaded on demand, the corresponding lblAccountID label for the particular row could be checked and used to set the selected value in the cboAccountID in a delayed manner.


            <telerik:GridTemplateColumn UniqueName="AccountID" SortExpression="AccountID" HeaderText="Account" ConvertEmptyStringToNull="true"
                    <ItemTemplate> 
                             <asp:Label ID="lblAccountID" runat="server" Text='<%# Eval("AccountID") %>' Width="305px" /> 
            <telerik:radcombobox id="cboAccountID" runat="server"   
                        Width="305px" Height="200px"  
                        EnableVirtualScrolling="true"  
                        HighlightTemplatedItems="true" 
                        EnableLoadOnDemand="true" 
                        DataValueField="AccountID" 
                        OnItemsRequested="cboAccountID_ItemsRequested"  
                        Style="display: none" 
                        EmptyMessage="Select a Account" > 
                        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                        <HeaderTemplate> 
                                    <table style="width: 275px" cellspacing="0" cellpadding="0"
                                        <tr> 
                                            <td style="width: 20%;"
                                                AccountID</td> 
                                            <td style="width: 80%"
                                                Account Alias and ControlSrc</td> 
                                            <td style="width: 20%"
                                                Is Active?</td> 
                                        </tr> 
                                    </table> 
                                </HeaderTemplate> 
                                <ItemTemplate> 
                                    <table style="width: 275px" cellspacing="0" cellpadding="0"
                                        <tr> 
                                            <td style="width: 20%;"
                                                <%#DataBinder.Eval(Container, "Attributes['AccountID']")%> 
                                            </td> 
                                            <td style="width: 80%;"
                                                <%#DataBinder.Eval(Container, "Attributes['AccountAliasAndControlSrc']")%> 
                                            </td> 
                                            <td style="width: 20%;"
                                                <%# DataBinder.Eval(Container, "Attributes['IsActive']")%> 
                                            </td> 
                                        </tr> 
                                    </table> 
                                </ItemTemplate> 
                        </telerik:radcombobox> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                 

Code-behind:
   Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound 
 
        Dim cbo As RadComboBox = e.Item.FindControl("cboAccountID"
        If Not cbo Is Nothing Then 
            If Not DataBinder.Eval(e.Item.DataItem, "AccountID"Is Nothing And IsDBNull(DataBinder.Eval(e.Item.DataItem, "AccountID")) = False Then 
                Try 
                    cbo.SelectedValue = DataBinder.Eval(e.Item.DataItem, "AccountID").ToString 
                Catch ex As ArgumentOutOfRangeException 
 
                Catch ex As Exception 
 
                End Try 
  
            End If 
        End If 
 
        Dim lbl As Label = e.Item.FindControl("lblAccountID"
        If Not lbl Is Nothing Then 
            If Not DataBinder.Eval(e.Item.DataItem, "AccountID"Is Nothing And IsDBNull(DataBinder.Eval(e.Item.DataItem, "AccountID")) = False Then 
                If Not String.IsNullOrEmpty(lbl.Text) Then 
                    Me.ToolTipManager.TargetControls.Add(lbl.ClientID, DataBinder.Eval(e.Item.DataItem, "AccountID").ToString, True
                End If 
            End If 
        End If 
    End Sub 

Protected Sub cboAccountID_ItemsRequested(ByVal sender As ObjectByVal e As RadComboBoxItemsRequestedEventArgs) 
        Dim reader As SqlDataReader = SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString, _ 
                        Data.CommandType.StoredProcedure, "proc_ConfigAccounts_GetAllAccounts"
 
        Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox) 
        comboBox.Items.Clear() 
        comboBox.ClearSelection() 
        While reader.Read 
            Dim item As New RadComboBoxItem() 
 
            item.Text = reader("AccountID").ToString & " | " & reader("AccountAliasAndControlSrc") & " | " & reader("IsActive").ToString 
            item.Value = reader("AccountID"
 
            Dim AccountAliasAndControlSrc As String = reader("AccountAliasAndControlSrc"
            Dim isActive As Boolean = reader("IsActive"
 
            item.Attributes.Add("AccountID", reader("AccountID").ToString) 
 
            item.Attributes.Add("AccountAliasAndControlSrc", AccountAliasAndControlSrc.ToString()) 
            item.Attributes.Add("IsActive", isActive.ToString()) 
 
            comboBox.Items.Add(item) 
            item.DataBind() 
        End While 
        reader.Close() 
 
 
        ' ''Add default list item 
        ''Dim DefaultItem As New RadComboBoxItem() 
        ''DefaultItem.Text = TryCast(GetGlobalResourceObject("CommonTerms", "EmptyDropdownItem"), String) 
        ''DefaultItem.Value = String.Empty 
        ''comboBox.Items.Insert(0, DefaultItem) 
    End Sub 

1 Answer, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 23 Jun 2010, 01:37 PM
Hi jgill,

The solution you have suggested is exactly the right one for this case.

You could handle the client-side ItemsRequested event of the RadComboBox and assuming that you have AccountID value previously selected for this row you could do the following:
function onItemsRequested(sender) {
    var itemToSelect = sender.findItemByValue(accountId);
    if (itemToSelect)
        itemToSelect.select();
}

All the best,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
ComboBox
Asked by
jgill
Top achievements
Rank 1
Answers by
Simon
Telerik team
Share this question
or