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

Binding ComboBox with ItemDataBound and ItemTemplate

4 Answers 315 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
msigman
Top achievements
Rank 2
msigman asked on 01 Mar 2011, 05:18 AM
I don't feel like this is adequately called out in the Telerik documentation and it caused me some grief, so I thought I would post it here for others who may be stuck in the same situation.

When binding a RadComboBox with a custom ItemTemplate and attempting to use ItemDataBound, it will not work.  You will get an empty template even though if you trace through with the debugger you see values in the ItemDataBound event.

The solution is to avoid using the ItemDataBound when using an ItemTemplate.  Instead you'll need to manually create the RadComboBoxItems inside the ItemsRequested event.

protected void IncrementalSearchEvent(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    ClientBLL clientBLL = new ClientBLL();
    var clients = clientBLL.IncrementalSearch(e.Text, Int32.Parse(rdoStatus.SelectedValue), 4);
 
    foreach (NameValuePair client in clients)
    {
        CachePerson obj = ObjectTools.GetObjectPerson(client.Value);
        RadComboBoxItem item = new RadComboBoxItem();
        item.Attributes["FirstName"] = obj.FirstName;
        item.Attributes["LastName"] = obj.LastName;
 
        item.Text = obj.FullName;
        item.Value = obj.PersonId.ToString();
 
        selSelectedClient.Items.Add(item);
    }
 
    selSelectedClient.DataBind();
}

<telerik:RadComboBox ID="selSelectedClient" runat="server" Skin="Telerik" EnableLoadOnDemand="true"
    onitemsrequested="IncrementalSearchEvent">
 
    <HeaderTemplate>
        <table border="0" width="590">
            <tr>
                <td width="100">First Name</td>
                <td width="100">Last Name</td>
            </tr>
        </table>
    </HeaderTemplate>
 
    <ItemTemplate>
        <table border="0" width="590"><tr>
        <td width="100"><%# DataBinder.Eval(Container, "Attributes['FirstName']")%></td>
        <td width="100"><%# DataBinder.Eval(Container, "Attributes['LastName']")%></td>
        </tr>
        </table>
    </ItemTemplate>
 
</telerik:RadComboBox>

4 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 07 Mar 2011, 11:17 AM
Hello Msigman,

The general idea behind load on demand is, that each time you request items they are loaded into the combobox. Thus ItemDataBound event is not fired, because items are not bound permanently to the combobox, but loaded upon user request. As you have noticed in such scenarios like yours, when using templates you should get use of the OnItemsRequested event and  add all items manually as you have mentioned. What bothers me in your provided code is that you are calling DataBind event for the whole combobox, but you should call DataBind for each item like shown bellow:
Copy Code
Copy Code
protected void IncrementalSearchEvent(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    ClientBLL clientBLL = new ClientBLL();
    var clients = clientBLL.IncrementalSearch(e.Text, Int32.Parse(rdoStatus.SelectedValue), 4);
 
    foreach (NameValuePair client in clients)
    {
        CachePerson obj = ObjectTools.GetObjectPerson(client.Value);
        RadComboBoxItem item = new RadComboBoxItem();
        item.Attributes["FirstName"] = obj.FirstName;
        item.Attributes["LastName"] = obj.LastName;
 
        item.Text = obj.FullName;
        item.Value = obj.PersonId.ToString();
 
        selSelectedClient.Items.Add(item);
        item.DataBind();
    }      
}

Kind regards,
Dimitar Terziev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
msigman
Top achievements
Rank 2
answered on 07 Mar 2011, 04:59 PM
That's odd -- the code I posted is working in our application exactly as-is... Including databinding on the combobox after all iterms are added to it.  I tried changing it to the way you suggested and it also works.  There is no observable difference between the two but I will leave it the way you suggested assuming it is the "best practices" method recommended by Telerik.

Also, the ItemDataBound event was fired for each item with LoadOnDemand=true (we even successfully use it in other, non templated scenarios in this project).  And it even has the correct values at that point, however after those events fire and the databinding begins to occur on the ASPX page, all the values that were assigned in ItemDatabound are lost.  This is the source of the problem.  It works though as long as you don't use an <ItemTemplate>.  Not sure why it seems to be working differently for us than the way you are describing.  However, everything is working fine so there is not an issue.
0
Dimitar Terziev
Telerik team
answered on 10 Mar 2011, 05:20 PM
Hi Msigman,

I'm glad that now your scenario is working as it should be.

Best wishes,
Dimitar Terziev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
msigman
Top achievements
Rank 2
answered on 10 Mar 2011, 05:26 PM
Thanks, I'm glad too.  Just to be clear for anyone viewing the thread in the future, I didn't make any changes here, my original solution was in the first post.  I was merely clearing up some ambiguity in the Telerik documentation.  This was not a troubleshooting/help thread; I'm providing the solution.
Tags
ComboBox
Asked by
msigman
Top achievements
Rank 2
Answers by
Dimitar Terziev
Telerik team
msigman
Top achievements
Rank 2
Share this question
or