paip.PHP-asp—jsp implements event mechanism WEBFORM style development
Foreword... 1
CODE Behind code separation... 1
Page controls... 1
Implement a form... 2
Realize state VIEWSTATE saving... 2
Page_Ini event and Page_Load event... 3
Implement button1_click event... 4
Implement button2_click event... 4
Note: webform.CodeFile.php source code... 5
Foreword
We all know that asp.net is developed in the WEBFORM style, which is easy to understand and based on the event mechanism. The development efficiency is much faster than the MVC method.
PHP, ASP, and JSP can also be developed using WEBFORM. Here we use PHP as an example to illustrate how to develop WEBFORM-style development..
CODE Behindcode separation
If you need to achieve code separation, in addition to MVC, you can also use Code-Behind technology to achieve it. It is simpler and the development efficiency is much faster than MVC. And it is easy to achieve modularization and componentization
In me we implement two pages, one for the interface HTML code, named webform.php, and one for the code, named webform.CodeFile.php
To implement CODE Behind, add the following code in the first line of webform.php:
Page Controls
In ASP. NET, we use RUNAT="SERVER" to indicate that an HTML control can be referenced on the server side. In PHP, a workaround is needed to achieve
LABEL control:
Textbox control:
Implement a form
Here we need a LABLE, a TEXTBOX control, and two button controls..
Our requirement is that when the first button is clicked, the LABLE and TEXTBOX values are set to button1 click…
When the second button is clicked, set the LABLE value to the input value in TEXTBOX
The total code is as follows
Implement state VIEWSTATE saving
In the CODE Behind file, webform.CodeFile.php.. We write code to save the state of the front-end interface control. . When the interface returns after submission, the control values are all in..
//Keep control status, viewstate management
viewstate();
//__VIEWSTATE
function viewstate()
{
foreach ($_REQUEST as $color){
$key=key($_REQUEST);
$controlName=$key."_Text";
// echo ($key."---".$color."
");
global $$controlName;
$$controlName =$color;
next($_REQUEST);
}
}
Page_Ini event and Page_Load event
When we access this form for the first time, the Page_Ini event is triggered. Every time we access this page, the Page_Load event is always triggered..
//Page event registration
eventReg4Page();
function Page_Ini()
{
echo "page ini event ";
global $Label1_Text;
$Label1_Text=" Page_Ini click";
global $TextBox1_Text;
$TextBox1_Text=" Page_Ini click";
}
function Page_Load()
{
echo "page load event ";
}
Implement button1_click event
//Control event registration
eventReg("Button1",Button1_Click);
//Click event of button control Button1
function Button1_Click()
{
global $TextBox1_Text;
$TextBox1_Text=" button1 click";
global $Label1_Text;
$Label1_Text=" button1 click";
}
//Event registration
function eventReg($controlName,$controlEvent)
{
if($_POST[$controlName])
$controlEvent();
}
Implement button2_click event
//Click event of button control Button2
function Button2_Click()
{
global $TextBox1_Text;
// $TextBox1_Text=" button2 click";
global $Label1_Text;
$Label1_Text=$TextBox1_Text;
}
Note: webform.CodeFile.php source code
//Keep control state, viewstate management
viewstate();
//Control event registration
eventReg("Button1",Button1_Click);
eventReg("Button2",Button2_Click);
//Page event registration
eventReg4Page();
function Page_Ini()
{
echo "page ini event ";
global $Label1_Text;
$Label1_Text=" Page_Ini click";
global $TextBox1_Text;
$TextBox1_Text=" Page_Ini click";
}
function Page_Load()
{
echo "page load event ";
}
//Click event of button control Button1
function Button1_Click()
{
global $TextBox1_Text;
$TextBox1_Text=" button1 click";
global $Label1_Text;
$Label1_Text=" button1 click";
}
//Click event of button control Button2
function Button2_Click()
{
global $TextBox1_Text;
// $TextBox1_Text=" button2 click";
global $Label1_Text;
$Label1_Text=$TextBox1_Text;
}
//-----------------The following functions can be included as public functions------------------ -
//Event registration
function eventReg($controlName,$controlEvent)
{
if($_POST[$controlName])
$controlEvent();
}
//Page event registration
function eventReg4Page()
{
if(!$_POST)
{
if(function_exists("Page_Ini"))
call_user_func("Page_Ini");
}
//Register Page_Load event
if(function_exists("Page_load"))
call_user_func("Page_load");
}
//__VIEWSTATE
function viewstate()
{
foreach ($_REQUEST as $color){
$key=key($_REQUEST);
$controlName=$key."_Text";
echo ($key."---".$color."
");
global $$controlName;
$$controlName =$color;
next($_REQUEST);
}
}
?>