Assign RadGrid to RadComboBox.ItemTemplate programatically

5 posts, 1 answers
  1. Kenneth
    Kenneth avatar
    3 posts
    Member since:
    Oct 2012

    Posted 17 Feb 2015 Link to this post

    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
  2. Aneliya Petkova
    Admin
    Aneliya Petkova avatar
    207 posts

    Posted 18 Feb 2015 Link to this post

    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.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kenneth
    Kenneth avatar
    3 posts
    Member since:
    Oct 2012

    Posted 04 Mar 2015 Link to this post

    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
  5. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 09 Mar 2015 Link to this post

    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.

     
  6. Kenneth
    Kenneth avatar
    3 posts
    Member since:
    Oct 2012

    Posted 10 Mar 2015 in reply to Viktor Tachev Link to this post

    Thanks Viktor, I see I should be adding the Control on each postback.
    Thanks again.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017