5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Web.UI.WebControls
15 * TLabel displays a piece of text on a Web page.
16 * Use {@link setText Text} property to set the text to be displayed.
17 * TLabel will render the contents enclosed within its component tag
18 * if {@link setText Text} is empty.
19 * To use TLabel as a form label, associate it with a control by setting the
20 * {@link setForControl ForControl} property.
21 * The associated control must be locatable within the label's naming container.
22 * If the associated control is not visible, the label will not be rendered, either.
24 * Note, {@link setText Text} will NOT be encoded for rendering.
25 * Make sure it does not contain dangerous characters that you want to avoid.
27 * @author Qiang Xue <qiang.xue@gmail.com>
28 * @package System.Web.UI.WebControls
31 class TLabel extends TWebControl implements IDataRenderer
33 private $_forControl='';
36 * @return string tag name of the label, returns 'label' if there is an associated control, 'span' otherwise.
38 protected function getTagName()
40 return ($this->getForControl()==='')?'span':'label';
44 * Adds attributes to renderer.
45 * @param THtmlWriter the renderer
46 * @throws TInvalidDataValueException if associated control cannot be found using the ID
48 protected function addAttributesToRender($writer)
50 if($this->_forControl!=='')
51 $writer->addAttribute('for',$this->_forControl);
52 parent::addAttributesToRender($writer);
57 * It overrides the parent implementation by checking if an associated
58 * control is visible or not. If not, the label will not be rendered.
59 * @param THtmlWriter writer
61 public function render($writer)
63 if(($aid=$this->getForControl())!=='')
65 if($control=$this->findControl($aid))
67 if($control->getVisible(true))
69 $this->_forControl=$control->getClientID();
70 parent::render($writer);
74 throw new TInvalidDataValueException('label_associatedcontrol_invalid',$aid);
77 parent::render($writer);
81 * Renders the body content of the label.
82 * @param THtmlWriter the renderer
84 public function renderContents($writer)
86 if(($text=$this->getText())==='')
87 parent::renderContents($writer);
89 $writer->write($text);
93 * @return string the text value of the label
95 public function getText()
97 return $this->getViewState('Text','');
101 * @param string the text value of the label
103 public function setText($value)
105 $this->setViewState('Text',$value,'');
109 * Returns the text value of the label.
110 * This method is required by {@link IDataRenderer}.
111 * It is the same as {@link getText()}.
112 * @return string the text value of the label
116 public function getData()
118 return $this->getText();
122 * Sets the text value of the label.
123 * This method is required by {@link IDataRenderer}.
124 * It is the same as {@link setText()}.
125 * @param string the text value of the label
129 public function setData($value)
131 $this->setText($value);
135 * @return string the associated control ID
137 public function getForControl()
139 return $this->getViewState('ForControl','');
143 * Sets the ID of the control that the label is associated with.
144 * The control must be locatable via {@link TControl::findControl} using the ID.
145 * @param string the associated control ID
147 public function setForControl($value)
149 $this->setViewState('ForControl',$value,'');