Find control in LayoutTemplate

6 posts, 0 answers
  1. Erik Kleinreesink
    Erik Kleinreesink avatar
    10 posts
    Member since:
    Nov 2008

    Posted 01 Feb 2010 Link to this post

    Please telle me how I can find controls in the LayoutTemplate (see attached picture).
    I need to set the text attributes for different languages.

  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 01 Feb 2010 Link to this post

    Hi Erik,

    Merely invoking the FindControl(labelId) method for the RadListView instance should be sufficient to locate the controls inside its layout template. This can be done inside the PageLoad handler of the page, for example:

    C#
    (RadListView1.FindControl("lblForum") as Label).Text = ....

    VB.NET
    CType(RadListView1.FindControl("lblForum"), Label).Text = .....


    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Erik Kleinreesink
    Erik Kleinreesink avatar
    10 posts
    Member since:
    Nov 2008

    Posted 01 Feb 2010 Link to this post

    Thank you for your answer.
    When I tried it, it didn't work. But I found out it depends on the page event in wich you use this statement.
    De control is not yet defined in the page_load event.
    When I used it in the RadListView1_PreRender event it worked fine.
  5. slnavn2000
    slnavn2000 avatar
    5 posts
    Member since:
    Nov 2008

    Posted 21 Jun 2010 Link to this post

    Hi All, 

    With this code, I could find control in LayoutTemplate,  
    protected void RadListView1_PreRender(object sender, EventArgs e)
            {            
                Label lblTest= RadListView1.FindControl("lblTest") as Label;
                lblTest.Text = "Hello";
            }

    but, when I using this to find control in EmptyDataTemplate, it return null, I don't know why?!!

    Please give me some suggestion!

    Thanks, 
    slnavn2000
  6. Kiara
    Kiara avatar
    309 posts
    Member since:
    Jul 2006

    Posted 21 Jun 2010 Link to this post

    Listen for the ItemCreated server event of the listview for this purpose. This code worked as expected on my end:

            <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
            <telerik:RadListView ID="RadListView1" DataSourceID="SqlDataSource1" runat="server" 
                ItemPlaceholderID="ProductItemContainer" Skin=""   
                onitemcreated="RadListView1_ItemCreated">  
                <LayoutTemplate> 
                    <fieldset id="ProductItemContainer" runat="server" /> 
                </LayoutTemplate> 
                <ItemTemplate> 
                    <ul> 
                        <li> 
                            <label> 
                                Product name:</label> 
                            <%#Eval("ProductName")%> 
                        </li> 
                        <li> 
                            <label> 
                                Quantity per unit:</label> 
                            <%#Eval("QuantityPerUnit")%> 
                        </li> 
                        <li> 
                            <label> 
                                Unit price:</label> 
                            <%# DataBinder.Eval(Container.DataItem, "UnitPrice", "{0:C}")%> 
                        </li> 
                        <li> 
                            <label> 
                                Units in stock:</label> 
                            <%#Eval("UnitsInStock")%> 
                        </li> 
                        <li> 
                            <label> 
                                Discontinued:</label> 
                            <%#Eval("Discontinued")%> 
                        </li> 
                    </ul> 
                </ItemTemplate> 
                <EmptyDataTemplate> 
                    <asp:Label ID="lblEmpty" runat="server" Text="No data" /> 
                </EmptyDataTemplate> 
            </telerik:RadListView> 
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP 3 ProductID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, Discontinued FROM Products WHERE 1=0" /> 

        protected void RadListView1_ItemCreated(object sender, RadListViewItemEventArgs e)  
        {  
            if (e.Item is RadListViewEmptyDataItem)  
            {  
                Label lbl1 = e.Item.FindControl("lblEmpty"as Label;  
                lbl1.Text = "No data available";  
            }  
        } 

    Kiara
  7. slnavn2000
    slnavn2000 avatar
    5 posts
    Member since:
    Nov 2008

    Posted 21 Jun 2010 Link to this post

    yeah, I can find control in ItemCreated event

    thanks Kiara very much :)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017