Edit this page

Finding Controls inside RadTimeView Templates

Sometimes it is necessary to locate a control contained inside a template. If the control is given an ID in the template, it can be retrieved from its container (the first control in the parent hierarchy that supports INamingContainer). Simply call the FindControl method of the container object to obtain a reference to the control in the template.

The most difficult case is locating a control inside the TimeTemplate or AlternatingTimeTemplate of a RadTimeView control. This is difficult because there are several controls with the same ID. However, each is contained logically in the namespace of the DataListItem control that is its container. You can access the container by using the DataList property of the RadTimeView control. You can then go through the data list's Items collection to retrieve the DataListItem for a given index. Once you have found the DataListItem of interest, call its FindControl method to retrieve a reference to the control in the template.


The following definitions declare a RadDateTimePicker that has a TimeTemplate defined, and a Button whose Click event handler needs to access a control in the template:

<telerik:RadDateTimePicker RenderMode="Lightweight" ID="RadDateTimePicker1" runat="server">
            <input style="width: 100%;"
            runat= "server"
            id= "Button1"
            value='<%# DataBinder.Eval(Container, "DataItem.Time", "{0:t}")%>' />
   Text="Change Color"
   OnClick="btnChangeColor_Click" />

In the code behind, the button's Click handler uses the FindControl method of the DataListItem to locate the control in the template:

protected void btnChangeColor_Click(object sender, EventArgs e)
    foreach (DataListItem dataListItem in RadDateTimePicker1.TimeView.DataList.Items)
        HtmlInputButton button = (HtmlInputButton)dataListItem.FindControl("Button1");
        button.Style.Add("background-color", "red");
Protected Sub btnChangeColor_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChangeColor.Click
    For Each dataListItem As DataListItem In RadDateTimePicker1.TimeView.DataList.Items
        Dim button As HtmlInputButton = DirectCast(dataListItem.FindControl("Button1"), HtmlInputButton)
        button.Style.Add("background-color", "red")
End Sub

See Also