Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > ComboBox > Load On Demand RadComboBox inside an EditItemTemplate of RadGrid

Answered Load On Demand RadComboBox inside an EditItemTemplate of RadGrid

Feed from this thread
  • Posted on Aug 18, 2010 (permalink)

    Requirements

    RadControls version RadComboBox for ASP.NET AJAX
    .NET version 2.0+
    Visual Studio version VS 2005/2008/2010
    programming language C#/VB.NET
    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    This sample project demonstrates how to preselect an item in RadComboBox with Load-On-Demand enabled and nested within the EditItemTemplate of RadGrid (or other parent databound control).

    THE ISSUE
    RadComboBox with Load-On-Demand enabled is often nested in EditItemTemplate of RadGrid or another data-bound control. 
    However preselecting a RadComboBoxItem at EditItemTemplate of the parent control in such scenario is a tricky task. Sometimes developers try to set the SelectedValue property of the RadComboBox via declarative databinding and receive this error: “Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.” 

    Please note that when the RadComboBox with Load-On-Demand feature initially loads - it is empty and it has no items. Load-On-Demand fires and populates the control with data when user types in the input area or clicks on the drop-down toggle image (if there are no items in the RadComboBox).  
    That is why when the parent control enters “Edit” mode – there are no items at the RadComboBox and trying to preselect an item raises an error.

    THE SOLUTION
    We can overcome this issue by handling the RadGrid.OnItemDatabound event and creating an initial RadComboBoxItem to be displayed at the control input. 

    [C#]
    protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            if (!(e.Item is IGridInsertItem))
            {
                RadComboBox combo =
                      (RadComboBox)item.FindControl("RadComboBox1");
                RadComboBoxItem preselectedItem = new RadComboBoxItem();
                preselectedItem.Text = item["CategoryName"].Text;
                preselectedItem.Value = item["CategoryID"].Text;
                combo.Items.Insert(0, preselectedItem);
                combo.SelectedIndex = 0;
     
            }
        }
    }

    [VB.NET]
    Protected Sub OnItemDataBoundHandler(ByVal sender As Object, ByVal e As GridItemEventArgs)
     
            If e.Item.IsInEditMode Then
                Dim item As GridEditableItem =
                    DirectCast(e.Item, GridEditableItem)
     
                If Not (TypeOf e.Item Is IGridInsertItem) Then
     
                    Dim combo As RadComboBox =
                   DirectCast(item.FindControl("RadComboBox1"), RadComboBox)
     
                    Dim preselectedItem As New RadComboBoxItem()
                    preselectedItem.Text = item("CategoryName").Text
                    preselectedItem.Value = item("CategoryID").Text
                    combo.Items.Insert(0, preselectedItem)
                    combo.SelectedIndex = 0
                End If
     
            End If
        End Sub

    When we delete the text initially displayed at RadComboBox input - Load-On-Demand will fire and will populate the control dropdown with items.

    In order to perform Add, Edit and Delete operation handle RadGrid.OnInsertCommandRadGrid.OnUpdateCommand and RadGrid.OnDeleteCommand events and obtain the RadComboBox selected value:
    [C#]
    protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
      
        GridEditableItem editedItem = e.Item as GridEditableItem;
        RadComboBox comboBox = (RadComboBox)editedItem.FindControl("RadComboBox1");
      
        SqlDataSource1.UpdateParameters.Add(
            new Parameter("CategoryID", DbType.Int32, comboBox.SelectedValue));
      
        SqlDataSource1.UpdateParameters.Add(
            new Parameter("UnitPrice", DbType.Double,
                (e.Item.FindControl("UnitPriceTextBox") as TextBox).Text));
      
        SqlDataSource1.UpdateParameters.Add(
            new Parameter("ProductName", DbType.String,
                (e.Item.FindControl("ProductNameBox") as TextBox).Text));
      
        SqlDataSource1.UpdateParameters.Add(
            new Parameter("ProductID", DbType.Int32,
                (e.Item.FindControl("ProductIDBox") as TextBox).Text));
      
        SqlDataSource1.Update();
      
    }
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        RadComboBox comboBox = (RadComboBox)editedItem.FindControl("RadComboBox1");
      
        SqlDataSource1.InsertParameters.Add(
            new Parameter("CategoryID", DbType.Int32, comboBox.SelectedValue));
              
          ...
          ...
                ...

        SqlDataSource1.Insert();
    }
      
    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    {
        GridDataItem item = (GridDataItem)e.Item;
        string ProductID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ProductID"].ToString();
      
        SqlDataSource1.DeleteParameters.Add(
            new Parameter("ProductID", DbType.Int32, ProductID));
        SqlDataSource1.Delete();
      
    }

    [VB.NET]
    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)
      
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim comboBox As RadComboBox = DirectCast(editedItem.FindControl("RadComboBox1"), RadComboBox)
      
        SqlDataSource1.UpdateParameters.Add(New Parameter("CategoryID", DbType.Int32, comboBox.SelectedValue))
      
        SqlDataSource1.UpdateParameters.Add(New Parameter("UnitPrice", DbType.[Double], TryCast(e.Item.FindControl("UnitPriceTextBox"), TextBox).Text))
      
        SqlDataSource1.UpdateParameters.Add(New Parameter("ProductName", DbType.[String], TryCast(e.Item.FindControl("ProductNameBox"), TextBox).Text))
      
        SqlDataSource1.UpdateParameters.Add(New Parameter("ProductID", DbType.Int32, TryCast(e.Item.FindControl("ProductIDBox"), TextBox).Text))
      
        SqlDataSource1.Update()
      
    End Sub
    Protected Sub RadGrid1_InsertCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs)
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim comboBox As RadComboBox = DirectCast(editedItem.FindControl("RadComboBox1"), RadComboBox)
      
        SqlDataSource1.InsertParameters.Add(New Parameter("CategoryID", DbType.Int32, comboBox.SelectedValue))

          ...
          ...
                ...

        SqlDataSource1.Insert()
      
    End Sub
      
    Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim ProductID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("ProductID").ToString()
        SqlDataSource1.DeleteParameters.Add(New Parameter("ProductID", DbType.Int32, ProductID))
        SqlDataSource1.Delete()
      
    End Sub


    The same approach can be used to nest Load-On-Demand RadComboBox in EditItemTemplate of FormView or GridView – please find more details at the sample project attached. Passing the update parameters is implemented using ControlParameters (at the FormView example) and SessionParameter(GridView example).

    Reply

  • Phil Avella avatar

    Posted on Nov 16, 2010 (permalink)

    how to acheive this in RadListView instead of grid?

    Reply

  • Kalina Kalina admin's avatar

    Posted on Nov 23, 2010 (permalink)

    Hi Phil Avella,

    I prepared a sample for you in order to demonstrate how to preselect an item in RadComboBox with Load-On-Demand enabled and nested within the EditItemTemplate of RadListView.
    In fact the approach is basically the same as the one used in the Code Library project published above.
    You simply have to handle the OnItemDataBound event of the RadListView and set the RadComboBox preselected item within the event handler.
    Additionally please handle the OnItemInserting, OnItemUpdating and OnItemDeleting events in order to pass the parameters necessary for performing insert/update and delete operations.

    All the best,
    Kalina
    the Telerik team
    Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

    Reply

  • Marcus avatar

    Posted on Jul 1, 2011 (permalink)

    Hi There I think this is a great article and am using it in my latest project. But I have a problem.

    I am using the 

    Protected Sub rcb_vehicleModel_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
     
            Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox)
     
            Dim dv As DataView = DirectCast(sqlds_vehicleModel.Select(DataSourceSelectArguments.Empty), DataView)
     
            comboBox.DataSource = dv
            comboBox.DataBind()

    'NOW I NEED TO CLEAR THE BELOW COMBO

     Dim cmdItem As GridItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0)
            Dim ddl As DropDownList = TryCast(cmdItem.FindControl("rcb_vehicleModel"), DropDownList)
            ddl.Items.Clear()

    *****DOES NOT WORK????
     
     End Sub


    event to load my combo and everything works fine. 
    
    But I need to clear some combos below as the selection, if altered in the to box, needs to be re-selected.
    
    Sounds simple but from the above Items Requested I cannot find the other combos in the edit template.
    
    Any suggestions...
    
    Marcus

    Reply

  • Kalina Kalina admin's avatar

    Posted on Jul 14, 2011 (permalink)

    Hello Marcus,


    I am not sure that I understand the issue.
    Why do you cast the RadComboBox "rcb_vehicleModel" to ASP.NET DropDownList(these two controls are absolutely different)?

    Greetings,
    Kalina
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

    Reply

  • License Developer avatar

    Posted on Sep 28, 2011 (permalink)

    Could you please show a sample project using a LoadOnDemand RadComboBox in a EditItemTemplate of a DetailsView control?
    Thank you. 

    Reply

  • License Developer avatar

    Posted on Sep 29, 2011 (permalink)

    Could you please show how to achieve this in a DetailsView instead of RadGrid?

    Thank you. 

    Reply

  • Kalina Kalina admin's avatar

    Posted on Oct 3, 2011 (permalink)

    Hello License Developer,

    The approach that you need to follow has been already implemented in the "FormView" sample page of the CodeLibrary project attached above.

    At first you have to handle the DetailsView.DataBound event and create an initial RadComboBoxItem to be displayed at the control input:
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        if(((DetailsView)sender).CurrentMode== DetailsViewMode.Edit)
        {
            DataRowView rowView =
                (DataRowView)((System.Web.UI.WebControls.DetailsView)(sender)).DataItem;
     
            RadComboBox combo =
                (RadComboBox)((System.Web.UI.WebControls.DetailsView)(sender)).FindControl("RadComboBox1");
            RadComboBoxItem preselectedItem = new RadComboBoxItem();
            preselectedItem.Text = rowView["CategoryName"].ToString();
            preselectedItem.Value = rowView["CategoryID"].ToString();
            combo.Items.Insert(0, preselectedItem);
            combo.SelectedIndex = 0;
        }
    }

    Then you can pass the update parameters to the SQLDataSource  in this way:
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
        SelectCommand="SELECT  P.ProductID,  P.ProductName, P.CategoryID, P.UnitPrice, C.CategoryName FROM Categories C INNER JOIN Products P  ON C.CategoryID = P.CategoryID ORDER BY  P.ProductID ASC "
        UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID, [UnitPrice] = @UnitPrice WHERE [ProductID] = @ProductID">
        <UpdateParameters>
            <asp:ControlParameter ControlID="DetailsView1" Name="ProductID" PropertyName="DataKey.Values['ProductID']"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ProductName" PropertyName="DataKey.Values['ProductName']"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1$RadComboBox1" Name="CategoryID" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="CategoryName" PropertyName="DataKey.Values['CategoryName']"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="UnitPrice" PropertyName="DataKey.Values['UnitPrice']"
                Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

    Please find the sample attached for further details.

    Greetings,
    Kalina
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
    Attached files

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > ComboBox > Load On Demand RadComboBox inside an EditItemTemplate of RadGrid