search
HomeBackend DevelopmentPHP Tutorialpaip.PHP-asp—jsp implements event mechanism WEBFORM style development_PHP tutorial

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

input:

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);

}

}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477976.htmlTechArticlepaip.PHP-aspjsp implements event mechanism WEBFORM development Preface... 1 CODE Behind code separation... 1 Page control... 1 Implement a form... 2 Implement state VIEWSTATE to save... 2 Page_Ini event and...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools