RadCalendar for ASP.NET

Finding a Control Inside a Template Send comments on this topic.
Time Picker > Finding a Control Inside a Template

Glossary Item Box

Sometimes it is necessary to locate a control contained inside a time cell. If a control is given an ID in a template, that control can be retrieved from its container (the first control in the parent hierarchy that supports INamingContainer). In this case, the container is the DataListItem control. Note that even though there are several controls with the same ID, each is contained logically in the namespace of the DataListItem container control.

You can go through the TimeView.DataList's Items collection to retrieve the DataListItem for a given index, and then call the DataListItem's FindControl method (inherited from the base Control class) to retrieve a control with a particular ID.

 

Example:

Default.aspx

ASPX Copy Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<
%@ Register Assembly="RadCalendar.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>

<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
   
<title>Untitled Page</title>
</
head>
<
body>
   
<form id="form1" runat="server">
       
<rad:RadTimePicker ID="RadTimePicker1" runat="server">
           
<TimeView>
               
<TimeTemplate>
                   
<input style="width: 100%;" runat="server" id="Button1" type="button" value='<%# DataBinder.Eval(Container, "DataItem.Time", "{0:t}")%>' />
               
</TimeTemplate>
           
</TimeView>
       
</rad:RadTimePicker>

       
<asp:Button ID="btnChangeColor" runat="server" Text="Change Color" OnClick="btnChangeColor_Click" />
   
</form>
</
body>
</
html>

 

Default.aspx.cs

C# Copy Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
   
protected void btnChangeColor_Click(object sender, EventArgs e)
   {
       
foreach (DataListItem dataListItem in RadTimePicker1.TimeView.DataList.Items)
       {
           HtmlInputButton button = (HtmlInputButton)dataListItem.FindControl(
"Button1");
           button.Style.Add(
"background-color", "red");
       }
   }
}