Add dynamically Itemtemplate on radCombobox which is inside RadGrid

4 posts, 0 answers
  1. Tanya
    Tanya avatar
    10 posts
    Member since:
    Apr 2014

    Posted 04 Jun 2014 Link to this post

    I've got RadCombobox with Itemtemplate (an anchor  element)  which is inside GridtemplateColumn of RadGrid. This RadCombobox has this properties set -  EnableLoadOnDemand="true" DataSourceID="ObjectDataSourceDrob" so  in RadGridLang_Loadevent  when I get to the point             pRadGridLang.DataBind();   myradcombobox is populated.

    My question is is there any way I can create dynamically this Itemtemplate od radcombobox dynamically and populate it with data
    Thank you

    Here is my aspx page

      <telerik:RadGrid ID="RadGridLang" runat="server" AutoGenerateColumns="False"  EnableEmbeddedSkins="False"  RegisterWithScriptManager=false 
                     DataTextField="Descr" DataValueField="LanImg"
                    Height="100%" Width="120px" AppendDataBoundItems="true" CellSpacing="0" GridLines="None" OnLoad="RadGridLang_Load">
                    <MasterTableView>
                        <Columns>
                            <telerik:GridTemplateColumn DataField="LanCd">
                                <ItemTemplate>
                                    <img src="<%#DataBinder.Eval(Container.DataItem, "LanImg")%>" alt="ASP.NET hosting" />
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn DataField="LanCd">
                                <ItemTemplate>
                                    <telerik:RadComboBox ID="RadComboBoxLang" runat="server" EmptyMessage="Select a language" DataTextField="Descr" DataValueField="LanCd" EnableEventValidation="true"  RegisterWithScriptManager=false 
                                        EnableLoadOnDemand="true" DataSourceID="ObjectDataSourceDrob" CausesValidation="false" AllowCustomText="true" AppendDataBoundItems="true" OnLoad="RadComboBoxLang_Load">
                                         <ItemTemplate>
                                            <div class="imageWrap">
                                               <<telerik:RadComboBoxItem Text="Select" />
                                                <a id='myhref' href='/<%#DataBinder.Eval(Container.DataItem, "Controller")%>/<%#DataBinder.Eval(Container.DataItem, "Action")%>/<%# Eval("LanCd") %>/<%#DataBinder.Eval(Container.DataItem, "menuID")%>'>
                                                <img src="<%#DataBinder.Eval(Container.DataItem, "LanImg")%>" alt='<%# Eval("Descr")%>'/>&nbsp&nbsp&nbsp&nbsp&nbsp<%#DataBinder.Eval(Container.DataItem, "Descr")%></a>
                                            </div>
                                           
                                        </ItemTemplate>
                                    </telerik:RadComboBox>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
                                                                                       
       



      <asp:ObjectDataSource ID="ObjectDataSourceDrob" runat="server" SelectMethod="GetWebLanguage" TypeName="MvcBeaDAL.WebServiceBea">
            <SelectParameters>
                <asp:Parameter DefaultValue="" Name="id" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>

    Here is my code 

    protected void RadComboBoxLang_Load(object sender, EventArgs e)
            {
                String pRequest = pGetLang; 
               RadComboBox radComboBoxLang = sender as RadComboBox;
               

                radComboBoxLang.SelectedValue = pRequest.ToUpper();
                bool p_test = MvcBeaDAL.CookiesManager.SetGetCookie(radComboBoxLang.SelectedValue);
            }
            protected void RadGridLang_Load(object sender, EventArgs e)
            {
                String pRequest = GetLang();
                pGetLang = pRequest;
                
                RadGrid pRadGridLang = this.FindControl("RadGridLang") as RadGrid;
                List<LanguageHost> radLang = new List<LanguageHost>();
                radLang.Add( MvcBeaDAL.WebServiceBea.GetWebLanguageID(pRequest));
               
                pRadGridLang.DataSource = radLang.ToList();
                pRadGridLang.DataBind();
            }
     
     
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Jun 2014 in reply to Tanya Link to this post

    Hi Tanya,

    Please try the following sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadGrid ID="RadGridLang" runat="server" AutoGenerateColumns="False" EnableEmbeddedSkins="False" DataSourceID="SqlDataSource1" Height="100%" Width="120px">
         <MasterTableView>
             <Columns>
                 <telerik:GridTemplateColumn DataField="OrderID">
                     <ItemTemplate>
                         <img src="<%#DataBinder.Eval(Container.DataItem, "OrderID")%>" alt="ASP.NET hosting" />
                     </ItemTemplate>
                 </telerik:GridTemplateColumn>
                 <telerik:GridTemplateColumn>
                     <ItemTemplate>
                         <telerik:RadComboBox ID="RadComboBoxLang" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerID" DataValueField="OrderID" EnableLoadOnDemand="true" OnItemDataBound="RadComboBoxLang_ItemDataBound" EnableEventValidation="true" AllowCustomText="true" AppendDataBoundItems="true">
                         </telerik:RadComboBox>
                     </ItemTemplate>
                 </telerik:GridTemplateColumn>
             </Columns>
         </MasterTableView>
     </telerik:RadGrid>

    C#:
    protected void RadComboBoxLang_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        Image img = new Image();
        img.Attributes.Add("src", e.Item.Text);
        img.Attributes.Add("alt", e.Item.Text);
        e.Item.Controls.Add(img);
    }

    Thanks,
    Shinu. 
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tanya
    Tanya avatar
    10 posts
    Member since:
    Apr 2014

    Posted 04 Jun 2014 in reply to Shinu Link to this post

    Unfortunately - it doesn't work! It doesn't add items to the radcomboboxc
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2014 in reply to Tanya Link to this post

    Hi Tanya,

    Please have a look into the sample code snippet to create the ItemTemplate(Image and Anchor ) of RadComboBox  from code behind. Let me know the sample code is working on your end.

    ASPX:
    <telerik:RadGrid ID="RadGridLang" runat="server" AutoGenerateColumns="False" EnableEmbeddedSkins="False" DataSourceID="SqlDataSource1" Height="100%" Width="120px">
        <MasterTableView>
            <Columns>
                <telerik:GridTemplateColumn DataField="OrderID">
                    <ItemTemplate>
                        <img src="<%#DataBinder.Eval(Container.DataItem, "OrderID")%>" alt="ASP.NET hosting" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadComboBox ID="RadComboBoxLang" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerID" DataValueField="OrderID" EnableAutomaticLoadOnDemand="true" OnItemDataBound="RadComboBoxLang_ItemDataBound" AllowCustomText="true" AppendDataBoundItems="true">
                        </telerik:RadComboBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadComboBoxLang_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        Image img = new Image();
        img.ID = "Image1";
        img.Attributes.Add("src", e.Item.Text);
        img.Attributes.Add("alt", e.Item.Text);
        e.Item.Controls.Add(img);
        HtmlAnchor htmlanchor = new HtmlAnchor();
        htmlanchor.HRef =e.Item.Text;
        htmlanchor.InnerText = e.Item.Text;
        e.Item.Controls.Add(htmlanchor);
    }

    As a suggestion from your code I have noticed that you are using OnLoad event to bind the Grid, the best approach to bind the RadGrid in OnNeedDataSource event.

    Thanks,
    Shinu.
Back to Top