TButton creates a click button on the page. It is mainly used to submit data to a page.
TButton raises two server-side events, {@link onClick OnClick} and {@link onCommand OnCommand},
when it is clicked on the client-side. The difference between these two events
is that the event {@link onCommand OnCommand} is bubbled up to the button's ancestor controls.
And within the event parameter for {@link onCommand OnCommand} contains the reference
to the {@link setCommandName CommandName} and {@link setCommandParameter CommandParameter}
property values that are set for the button object. This allows you to create multiple TButton
components on a Web page and programmatically determine which one is clicked
with what parameter.
Clicking on button can also trigger form validation, if
{@link setCausesValidation CausesValidation} is true.
The validation may be restricted within a certain group of validator
controls by setting {@link setValidationGroup ValidationGroup} property.
If validation is successful, the data will be post back to the same page.
TButton displays the {@link setText Text} property as the button caption.
TButton can be one of three {@link setButtonType ButtonType}: Submit, Button and Reset.
By default, it is a Submit button and the form submission uses the browser's
default submission capability. If it is Button or Reset, postback may occur
if one of the following conditions is met:
- an event handler is attached to {@link onClick OnClick} event;
- an event handler is attached to {@link onCommand OnCommand} event;
- the button is in a non-empty validation group.
In addition, clicking on a Reset button will clear up all input fields
if the button does not cause a postback.
/** * Creates a pager button. * Depending on the button type, a TLinkButton or a TButton may be created. * If it is enabled (clickable), its command name and parameter will also be set. * Derived classes may override this method to create additional types of buttons, such as TImageButton. * @param string button type, either LinkButton or PushButton * @param boolean whether the button should be enabled * @param string caption of the button. * @param string CommandName corresponding to the OnCommand event of the button. * @param string CommandParameter corresponding to the OnCommand event of the button * @return mixed the button instance */ protected function createPagerButton($buttonType, $enabled, $text, $commandName, $commandParameter) { if ($buttonType === TPagerButtonType::LinkButton) { if ($enabled) { $button = new TLinkButton(); } else { $button = new TLabel(); $button->setText($text); $button->setCssClass($this->getButtonCssClass()); return $button; } } else { if ($buttonType === TPagerButtonType::ImageButton) { $button = new TImageButton(); $button->setImageUrl($this->getPageImageUrl($text, $commandName)); } else { $button = new TButton(); } if (!$enabled) { $button->setEnabled(false); } } $button->setText($text); $button->setCommandName($commandName); $button->setCommandParameter($commandParameter); $button->setCausesValidation(false); $button->setCssClass($this->getButtonCssClass()); return $button; }
/** * Creates a pager button. * Depending on the button type, a TLinkButton or a TButton may be created. * If it is enabled (clickable), its command name and parameter will also be set. * Derived classes may override this method to create additional types of buttons, such as TImageButton. * @param mixed the container pager instance of TActiveDatagridPager * @param string button type, either LinkButton or PushButton * @param boolean whether the button should be enabled * @param string caption of the button * @param string CommandName corresponding to the OnCommand event of the button * @param string CommandParameter corresponding to the OnCommand event of the button * @return mixed the button instance */ protected function createPagerButton($pager, $buttonType, $enabled, $text, $commandName, $commandParameter) { if ($buttonType === TDataGridPagerButtonType::LinkButton) { if ($enabled) { $button = new TLinkButton(); } else { $button = new TLabel(); $button->setText($text); return $button; } } else { $button = new TButton(); if (!$enabled) { $button->setEnabled(false); } } $button->setText($text); $button->setCommandName($commandName); $button->setCommandParameter($commandParameter); $button->setCausesValidation(false); return $button; }
/** * Ensure that the ID attribute is rendered and registers the javascript code * for initializing the active control. */ protected function addAttributesToRender($writer) { parent::addAttributesToRender($writer); $writer->addAttribute('id', $this->getClientID()); $this->getActiveControl()->registerCallbackClientScript($this->getClientClassName(), $this->getPostBackOptions()); }