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

Issue with AutoPostBack RadComboBox inside RadGrid GridTemplateColumn

3 Answers 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ed
Top achievements
Rank 1
Ed asked on 29 Aug 2011, 05:02 PM
I'm trying to add a AutoPostBack enabled RadComboBox to the ItemTemplate of a RadGrid's GridTemplateColumn.  The RadComboBox relies on certain row data so I'm creating it dynamically using the grid's ItemDataBound event:

protected void rgFaqs_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Item || e.Item.ItemType == Telerik.Web.UI.GridItemType.AlternatingItem)
    {
        FaqListItem item = (FaqListItem)e.Item.DataItem;
 
        if (item == null)
            return;
 
        WorkflowComboBox workflowBox = new WorkflowComboBox();
        workflowBox.ItemID = item.Id;
        workflowBox.DataBind();
 
        PlaceHolder phWorkflowBox = (PlaceHolder)e.Item.FindControl("phWorkflowBox");
        phWorkflowBox.Controls.Add(workflowBox);
    }
}

Here is a snippet of my custom overridden WorkflowComboBox:

    public class WorkflowComboBox : Telerik.Web.UI.RadComboBox
    {
        public int ItemID { get; set; }
 
        public WorkflowComboBox()
        {
            AutoPostBack = true;
        }
 
        protected override void OnInit(EventArgs e)
        {
            this.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(WorkflowComboBox_SelectedIndexChanged);
 
            EnsureChildControls();
            base.OnInit(e);
        }

Like I said, this works perfectly fine on the initial page load.  The problem occurs on the AutoPostBack of the ComboBox.  This causes a postback which doesn't appear to be firing the ItemDataBound event again.  That in turn causes my the Combobox field to not be loaded properly.

I've also tried moving the ItemDataBound stuff to the RadGrid's ItemCreated event instead.  Everything works fine on first page load. When the ComboBox AutoPostBack gets fired though, the e.Item.DataItem is null. 

I'm getting the vibe that this is an issue/conflict? with the event sequence of the RadGrid and the RadComboBox.  It also appears during this whole process that the RadComboBox SelectedIndexChanged event is never being reached.

Any thoughts?  Am I missing something!?

-Thanks

3 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 01 Sep 2011, 09:47 AM
Hello Ed,

Please refer to this online help topic explaining the major differences between the ItemCreated and ItemDataBound events. The main point is that ItemCreated fires before databinding and it fires on every postback. This means the event handler is the correct point for initializing and dynamically adding controls to the grid. ItemDataBound, on its side, is fired after the item is databound, meaning you have access to e.Item.DataItem and can use it to bind your own custom controls inside.

Bottom line is, you need to create your custom combo in the ItemCrated event and bind it in the ItemDataBound event. You cannot use only one or the other.

Greetings,
Veli
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Gabe
Top achievements
Rank 1
answered on 27 Dec 2012, 02:27 PM
There doesn't seem to be a link to this demo.  I'm having a similar problem with sequence of events.

I'm using a RadComboBox in a child radgrid.  When I change the combobox selection, and it does postback, the content refreshes with the detail from the parent grid item (reverts to DB data for the item).  The ComboBox is built in the EditItemTemplate, not in the .cs, so I don't know that I can build the box in the ItemCreated.  I already simply bind in the ItemDataBound call.  

My issue is with the "rcbFullName" combobox.

Any help would be appreciated.

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode)
    {
        RadComboBox combo1 = e.Item.FindControl("CategoryCombo") as RadComboBox;
        //RadComboBox combo2 = e.Item.FindControl("SubcategoryCombo") as RadComboBox;
        RadComboBox employeename = e.Item.FindControl("rcbFullName") as RadComboBox;
        combo1.DataSource = CategoryBL.GetAllCategories();
        combo1.DataBind();
 
        combo1.SelectedValue = ((ProductDTO)e.Item.DataItem).CategoryId.ToString();
 
        employeename.DataSource = EmployeeBL.GetAllEmployees();
        employeename.DataBind();
        foreach (RadComboBoxItem item in employeename.Items)
        {
            if (item.Value == ((ProductDTO)e.Item.DataItem).EmployeeId.ToString()) { item.Selected = true; }
        }
    }
}
0
Maria Ilieva
Telerik team
answered on 02 Jan 2013, 01:12 PM
Hi Gabe,

Here is the help topic my colleague Veli pointed to:

http://www.telerik.com/help/aspnet-ajax/grid-distinguish-differences-between-itemcreated-itemdatabound.html

Please ensure that the problematic combobox  is not also bind declaratively in the template mark-up using the DataSource property. If this is not the source of the issue I would suggest you to open a regular support ticket and send us sample runnable application which demonstrates the problematic behavior. Thus we will be able to debug it locally and advise you further.

Greetings,
Maria Ilieva
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.
Tags
Grid
Asked by
Ed
Top achievements
Rank 1
Answers by
Veli
Telerik team
Gabe
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or