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

Assign RadGrid to RadComboBox.ItemTemplate programatically

4 Answers 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 1
Kenneth asked on 17 Feb 2015, 06:22 PM
Hi,
I'm looking at the sample that embeds a RadGrid in a RadComboBox (http://www.telerik.com/help/aspnet-ajax/grid-in-combobox.html) and I see that you can add the RadGrid to the RadComboBox declaratively in the aspx:
<!-- edited from sample, aspx simplifies to this:  -->
<telerik:RadComboBox ID="RadComboBox1" runat="server" >
  <ItemTemplate>
    <telerik:RadGrid ID="RadGrid1" runat="server">
...
    </telerik:RadGrid>
  </ItemTemplate>
</telerik:RadComboBox>

I need to be able to do this in the code behind and I can't figure out how to set the ItemTemplate to a RadGrid properly given it must be an ITemplate:

protected void Page_Init(object sender, EventArgs e)
{
    //create basic grid
    RadGrid grid = new RadGrid();
    grid.ID = "RadGrid1";
    grid.NeedDataSource += RadGrid1_NeedDataSource;
    grid.AllowSorting = true;
    grid.MasterTableView.AutoGenerateColumns = true;
 
    //try mimic aspx page implementation... not possible because of Type mismatch
    RadComboBox1.ItemTemplate = grid;
}


Is it possible to do this in the code behind?

Thanks,
Ken

4 Answers, 1 is accepted

Sort by
0
Aneliya Petkova
Telerik team
answered on 18 Feb 2015, 11:15 AM
Hi Ken,

Please check the following help article showing how you can add templates to RadComboBox at runtime (see 'Adding Templates at runtime' section):
http://www.telerik.com/help/aspnet-ajax/combobox-templates-adding.html

Hope this will be helpful.

Regards,
Aneliya Petkova
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kenneth
Top achievements
Rank 1
answered on 04 Mar 2015, 03:08 PM
Thanks for that Aneliya, I can embed a Grid into the Combobox by implementing an ITemplate class.
I have a few follow questions:

In the example at http://www.telerik.com/help/aspnet-ajax/grid-in-combobox.html when you sort the Grid by a column you can see a POST (through the RadAjaxManager I'm assuming) & the content is resorted as expected.
If I replace the aspx RadGrid inside the RadComboBox with the ITemplate implementation I don't see any POST on clicking the column headers & the page is refetched completely, is there an extra step to get that functioning given that my GridTemplate looks like this:

class GridTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.AllowSorting = true;
        grid.MasterTableView.AutoGenerateColumns = true;
        grid.ClientSettings.ClientEvents.OnRowClick = "RowClicked";
        grid.ClientSettings.Scrolling.AllowScroll = true;
        grid.ClientSettings.Scrolling.UseStaticHeaders = true;
 
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(NeedDataSourceEvent);
 
        container.Controls.Add(grid);
    }
 
//implementation of NeedDataSourceEvent here...
}



Also I have some questions about the AjaxManager, if I want to assign a Control as being updated over AJAX on the server side is this:
//assume there are two comboBoxes, RadComboBox1 & RadComboBox2
var settings = new AjaxSetting("settings");
settings.UpdatedControls.Add(new AjaxUpdatedControl { ControlID = "RadComboBox1" });
settings.UpdatedControls.Add(new AjaxUpdatedControl { ControlID = "RadComboBox2" });
RadAjaxManager1.AjaxSettings.Add(settings);

the programmatic equivalent of:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
       <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadComboBox1"/>
                    <telerik:AjaxUpdatedControl ControlID="RadComboBox2"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
</telerik:RadAjaxManager>

Because I can't update the controls when I try do it programatically.



I have a scenario where I have RadComboBox that is populated through a <WebServiceSettings> tag and the method takes a RadComboBoxContext as a parameter (populated in the javascript with arguments).
If I wanted to use a RadGrid to contain the previous contents of the ComboBox (to allow sorting) can I attach the webservice programatically to the RadGrid and have the equivalent of the RadComboBoxContext passed to the webservice in the same way?



Thank you,
Ken
0
Accepted
Viktor Tachev
Telerik team
answered on 09 Mar 2015, 01:01 PM
Hello Ken,

For your convenience I have prepared a sample project where the functionality is implemented. There is a RadGrid that is added dynamically to RadComboBox. The paging and sorting in the grid is working as expected on my end.

The sample also illustrates how you can add AjaxSettings to RadAjaxManager programmatically. You can use the AddAjaxSetting method to easily set the AJAX initiator and the updated control.

The project is attached to this post. Give it a try and see how it works for you.

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kenneth
Top achievements
Rank 1
answered on 10 Mar 2015, 11:37 AM
Thanks Viktor, I see I should be adding the Control on each postback.
Thanks again.
Tags
General Discussions
Asked by
Kenneth
Top achievements
Rank 1
Answers by
Aneliya Petkova
Telerik team
Kenneth
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or