Paip. PHP-asp-jsp implements WEBFORM-based event mechanism development

Source: Internet
Author: User

Paip. PHP-asp-jsp implements WEBFORM-based event mechanism development

Preface... 1

CODE Behind CODE separation... 1

Page Control... 1

Implement a form... 2

Save status VIEWSTATE... 2

Page_Ini events and Page_Load events... 3

Implement the button#click event... 4

Implement the button2_click event... 4

Note: webform. CodeFile. php source code... 5

 

Preface
We all know that asp.net is developed based on WEBFORM, which is easy to understand and based on the event mechanism. The development efficiency is much faster than that of MVC ..

PHP, ASP, and JSP can also be developed using WEBFORM. Here we use PHP as an example to illustrate how to develop WEBFORM ..

 

CODE Behind CODE separation
To implement Code separation, in addition to MVC, Code-Behind technology can also be used for implementation, which is simpler and faster than MVC. It is also easy to implement modularization and componentization.

In my project, we implement two pages: one with HTML code on the interface, named webform. php, and the other with code named webform. CodeFile. php.

 

To implement CODE Behind, add the following CODE to the first line of webform. php:

<? Php require ('webform. CodeFile. php');?>

Page Control
In ASP. In. NET, we use RUNAT = "SERVER" to indicate that an HTML control can be referenced on the SERVER .. in PHP, it needs to be implemented in a flexible way.

LABEL control:

<Span> <? Phpecho ($ labelemeditext);?> </Span>

<Input type = "hidden" name = "Label1" value = "<? Phpecho ($ labelemeditext);?> "/>

 

Textbox Control:

<Input type = "text" name = "TextBox1" value = "<? Phpecho ($ textboxshorttext);?> "/>

 

Implement a form
Here we need a LABLE, a textbox Control, and two button controls ..

When you click the 1st button, set the LABLE and TEXTBOX values to button1 click...

When you click the second button, set the LABLE value to the input value in TEXTBOX.

The Code is as follows:

<? Php require ('webform. CodeFile. php');?>

 

<Form id = "form1" name = "form1" method = "post" action = "">

<P>

<Span> <? Phpecho ($ labelemeditext);?> </Span>

<Input type = "hidden" name = "Label1" value = "<? Phpecho ($ labelemeditext);?> "/>

</P>

<P> input:

<Input type = "text" name = "TextBox1" value = "<? Phpecho ($ textboxshorttext);?> "/>

</P>

<P>

<Input type = "submit" name = "Button1" value = "submit"/>

<Input type = "submit" name = "Button2" value = "submit 2"/>

</P>

</Form>

 

 

Save VIEWSTATE
In the CODE Behind file, we write CODE for webform. CodeFile. php... to save the control status on the front-end interface .. When the interface is submitted and the return result is ..

 

<? Php

// Maintain the control status and manage viewstate

Viewstate ();

 

// _ VIEWSTATE

Function viewstate ()

{

Foreach ($ _ REQUEST as $ color ){

$ Key = key ($ _ REQUEST );

$ ControlName = $ key. "_ Text ";

// Echo ($ key. "---". $ color. "<br> ");

Global $ controlName;

$ ControlName = $ color;

Next ($ _ REQUEST );

}

}

 

 

Page_Ini and Page_Load events
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 will always be triggered ..

 

// Page event registration

EventReg4Page ();

 

Function Page_Ini ()

{

 

Echo "page ini event ";

Global $ labelemeditext;

$ Labelemeditext = "Page_Ini click ";

Global $ textboxshorttext;

$ Textboxshorttext = "Page_Ini click ";

 

}

 

Function Page_Load ()

{

 

 

Echo "page load event ";

 

}

 

Implement the button#click event
 

// Control event registration

EventReg ("Button1", button#click );

 

// Button control Button1 Click Event

Function button#click ()

{

Global $ textboxshorttext;

$ Textboxshorttext = "button1 click ";

Global $ labelemeditext;

$ Labelemeditext = "button1 click ";

 

}

// Event registration

Function eventReg ($ controlName, $ controlEvent)

{

 

 

 

If ($ _ POST [$ controlName])

$ ControlEvent ();

 

 

}

 

Implement the button2_click event
// Button control Button2 Click Event

Function Button2_Click ()

{

Global $ textboxshorttext;

// $ Textboxshorttext = "button2 click ";

Global $ labelemeditext;

$ Labelshorttext = $ textboxshorttext;

 

}

 

Note: webform. CodeFile. php source code
 

<? Php

// Maintain the control status and manage viewstate

Viewstate ();

// Control event registration

EventReg ("Button1", button#click );

EventReg ("Button2", Button2_Click );

// Page event registration

EventReg4Page ();

 

 

 

Function Page_Ini ()

{

 

Echo "page ini event ";

Global $ labelemeditext;

$ Labelemeditext = "Page_Ini click ";

Global $ textboxshorttext;

$ Textboxshorttext = "Page_Ini click ";

 

}

 

Function Page_Load ()

{

 

 

Echo "page load event ";

 

}

 

// Button control Button1 Click Event

Function button#click ()

{

Global $ textboxshorttext;

$ Textboxshorttext = "button1 click ";

Global $ labelemeditext;

$ Labelemeditext = "button1 click ";

 

}

// Button control Button2 Click Event

Function Button2_Click ()

{

Global $ textboxshorttext;

// $ Textboxshorttext = "button2 click ";

Global $ labelemeditext;

$ Labelshorttext = $ textboxshorttext;

 

}

 

// ----------------- The following functions can be used as public functions, including -------------------

 

// 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 the 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. "<br> ");

Global $ controlName;

$ ControlName = $ color;

Next ($ _ REQUEST );

}

}

?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.