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 * TButton creates a click button on the page. It is mainly used to submit data to a page.
17 * TButton raises two server-side events, {@link onClick OnClick} and {@link onCommand OnCommand},
18 * when it is clicked on the client-side. The difference between these two events
19 * is that the event {@link onCommand OnCommand} is bubbled up to the button's ancestor controls.
20 * And within the event parameter for {@link onCommand OnCommand} contains the reference
21 * to the {@link setCommandName CommandName} and {@link setCommandParameter CommandParameter}
22 * property values that are set for the button object. This allows you to create multiple TButton
23 * components on a Web page and programmatically determine which one is clicked
24 * with what parameter.
26 * Clicking on button can also trigger form validation, if
27 * {@link setCausesValidation CausesValidation} is true.
28 * The validation may be restricted within a certain group of validator
29 * controls by setting {@link setValidationGroup ValidationGroup} property.
30 * If validation is successful, the data will be post back to the same page.
32 * TButton displays the {@link setText Text} property as the button caption.
34 * TButton can be one of three {@link setButtonType ButtonType}: Submit, Button and Reset.
35 * By default, it is a Submit button and the form submission uses the browser's
36 * default submission capability. If it is Button or Reset, postback may occur
37 * if one of the following conditions is met:
38 * - an event handler is attached to {@link onClick OnClick} event;
39 * - an event handler is attached to {@link onCommand OnCommand} event;
40 * - the button is in a non-empty validation group.
41 * In addition, clicking on a Reset button will clear up all input fields
42 * if the button does not cause a postback.
44 * @author Qiang Xue <qiang.xue@gmail.com>
45 * @package System.Web.UI.WebControls
48 class TButton extends TWebControl implements IPostBackEventHandler, IButtonControl, IDataRenderer
51 * @return string tag name of the button
53 protected function getTagName()
59 * @return boolean whether to render javascript.
61 public function getEnableClientScript()
63 return $this->getViewState('EnableClientScript',true);
67 * @param boolean whether to render javascript.
69 public function setEnableClientScript($value)
71 $this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
75 * Adds attribute name-value pairs to renderer.
76 * This overrides the parent implementation with additional button specific attributes.
77 * @param THtmlWriter the writer used for the rendering purpose
79 protected function addAttributesToRender($writer)
81 $page=$this->getPage();
82 $page->ensureRenderInForm($this);
83 $writer->addAttribute('type',strtolower($this->getButtonType()));
84 if(($uniqueID=$this->getUniqueID())!=='')
85 $writer->addAttribute('name',$uniqueID);
86 $writer->addAttribute('value',$this->getText());
87 if($this->getEnabled(true))
89 if($this->getEnableClientScript() && $this->needPostBackScript())
90 $this->renderClientControlScript($writer);
92 else if($this->getEnabled()) // in this case, parent will not render 'disabled'
93 $writer->addAttribute('disabled','disabled');
95 parent::addAttributesToRender($writer);
99 * Renders the client-script code.
101 protected function renderClientControlScript($writer)
103 $writer->addAttribute('id',$this->getClientID());
104 $this->getPage()->getClientScript()->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
108 * Gets the name of the javascript class responsible for performing postback for this control.
109 * This method overrides the parent implementation.
110 * @return string the javascript class name
112 protected function getClientClassName()
114 return 'Prado.WebUI.TButton';
118 * @return boolean whether to perform validation if the button is clicked
120 protected function canCauseValidation()
122 if($this->getCausesValidation())
124 $group=$this->getValidationGroup();
125 return $this->getPage()->getValidators($group)->getCount()>0;
132 * @param boolean set by a panel to register this button as the default button for the panel.
134 public function setIsDefaultButton($value)
136 $this->setViewState('IsDefaultButton', TPropertyValue::ensureBoolean($value),false);
140 * @return boolean true if this button is registered as a default button for a panel.
142 public function getIsDefaultButton()
144 return $this->getViewState('IsDefaultButton', false);
148 * @return boolean whether the button needs javascript to do postback
150 protected function needPostBackScript()
152 return $this->canCauseValidation() || ($this->getButtonType()!==TButtonType::Submit &&
153 ($this->hasEventHandler('OnClick') || $this->hasEventHandler('OnCommand')))
154 || $this->getIsDefaultButton();
158 * Returns postback specifications for the button.
159 * This method is used by framework and control developers.
160 * @return array parameters about how the button defines its postback behavior.
162 protected function getPostBackOptions()
164 $options['ID']=$this->getClientID();
165 $options['CausesValidation']=$this->getCausesValidation();
166 $options['EventTarget'] = $this->getUniqueID();
167 $options['ValidationGroup']=$this->getValidationGroup();
173 * Renders the body content enclosed between the control tag.
174 * This overrides the parent implementation with nothing to be rendered.
175 * @param THtmlWriter the writer used for the rendering purpose
177 public function renderContents($writer)
182 * This method is invoked when the button is clicked.
183 * The method raises 'OnClick' event to fire up the event handlers.
184 * If you override this method, be sure to call the parent implementation
185 * so that the event handler can be invoked.
186 * @param TEventParameter event parameter to be passed to the event handlers
188 public function onClick($param)
190 $this->raiseEvent('OnClick',$this,$param);
194 * This method is invoked when the button is clicked.
195 * The method raises 'OnCommand' event to fire up the event handlers.
196 * If you override this method, be sure to call the parent implementation
197 * so that the event handlers can be invoked.
198 * @param TCommandEventParameter event parameter to be passed to the event handlers
200 public function onCommand($param)
202 $this->raiseEvent('OnCommand',$this,$param);
203 $this->raiseBubbleEvent($this,$param);
207 * Raises the postback event.
208 * This method is required by {@link IPostBackEventHandler} interface.
209 * If {@link getCausesValidation CausesValidation} is true, it will
210 * invoke the page's {@link TPage::validate validate} method first.
211 * It will raise {@link onClick OnClick} and {@link onCommand OnCommand} events.
212 * This method is mainly used by framework and control developers.
213 * @param TEventParameter the event parameter
215 public function raisePostBackEvent($param)
217 if($this->getCausesValidation())
218 $this->getPage()->validate($this->getValidationGroup());
219 $this->onClick(null);
220 $this->onCommand(new TCommandEventParameter($this->getCommandName(),$this->getCommandParameter()));
224 * @return string caption of the button
226 public function getText()
228 return $this->getViewState('Text','');
232 * @param string caption of the button
234 public function setText($value)
236 $this->setViewState('Text',$value,'');
240 * Returns the caption of the button.
241 * This method is required by {@link IDataRenderer}.
242 * It is the same as {@link getText()}.
243 * @return string caption of the button.
247 public function getData()
249 return $this->getText();
253 * Sets the caption of the button.
254 * This method is required by {@link IDataRenderer}.
255 * It is the same as {@link setText()}.
256 * @param string caption of the button
260 public function setData($value)
262 $this->setText($value);
266 * @return boolean whether postback event trigger by this button will cause input validation, default is true
268 public function getCausesValidation()
270 return $this->getViewState('CausesValidation',true);
274 * @param boolean whether postback event trigger by this button will cause input validation
276 public function setCausesValidation($value)
278 $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
282 * @return string the command name associated with the {@link onCommand OnCommand} event.
284 public function getCommandName()
286 return $this->getViewState('CommandName','');
290 * @param string the command name associated with the {@link onCommand OnCommand} event.
292 public function setCommandName($value)
294 $this->setViewState('CommandName',$value,'');
298 * @return string the parameter associated with the {@link onCommand OnCommand} event
300 public function getCommandParameter()
302 return $this->getViewState('CommandParameter','');
306 * @param string the parameter associated with the {@link onCommand OnCommand} event.
308 public function setCommandParameter($value)
310 $this->setViewState('CommandParameter',$value,'');
314 * @return string the group of validators which the button causes validation upon postback
316 public function getValidationGroup()
318 return $this->getViewState('ValidationGroup','');
322 * @param string the group of validators which the button causes validation upon postback
324 public function setValidationGroup($value)
326 $this->setViewState('ValidationGroup',$value,'');
330 * @return TButtonType the type of the button. Defaults to TButtonType::Submit.
332 public function getButtonType()
334 return $this->getViewState('ButtonType',TButtonType::Submit);
338 * @param TButtonType the type of the button.
340 public function setButtonType($value)
342 $this->setViewState('ButtonType',TPropertyValue::ensureEnum($value,'TButtonType'),TButtonType::Submit);
348 * TButtonType defines the enumerable type for the possible types that a {@link TButton} can take.
350 * The following enumerable values are defined:
351 * - Submit: a normal submit button
352 * - Reset: a reset button
353 * - Button: a client button (normally does not perform form submission)
355 * @author Qiang Xue <qiang.xue@gmail.com>
356 * @package System.Web.UI.WebControls
359 class TButtonType extends TEnumerable
361 const Submit='Submit';
363 const Button='Button';