• 5109阅读
  • 0回复

[PHP]在PHP中模拟ASP.NET事件驱动和状态保持的简单模型 [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2005-07-30
<?php
class Page_T
{
  private $isPostBack;
  public $ViewState;
  function __construct()
  {
      $this->isPostBack = false;
      if (isset($_POST['__EVENTTARGET']) && $_POST['__EVENTTARGET'] != "")
      {
          $this->isPostBack = true;
      }
      $this->ViewState = $_POST;
      unset($this->ViewState['__VIEWSTATE']);
      unset($this->ViewState['__EVENTTARGET']);
      unset($this->ViewState['__EVENTARGUMENT']);    
  }

  function isPostBack()
  {
      return $this->isPostBack;
  }
 
  function Run()
  {
      if ($this->isPostBack())
      {
          $this->$_POST['__EVENTTARGET']($_POST['__EVENTARGUMENT']);
      }
  }
}

class WebForm1_T extends Page_T
{
  function __construct()
  {
      parent::__construct();
  }
  function Page_Load()
  {
      if (!$this->isPostBack())
      {
          $this->ViewState['TextArea1'] = "现在的时间是: " . date("Y-m-d H:i:s");
      }
  }
 
  function Button2Click($EventArgument)
  {
      $this->ViewState['TextBox1'] = date("Y-m-d H:i:s");
  }
}

$WebForm1 = new WebForm1_T();
$WebForm1->Page_Load();
$WebForm1->Run();

?>

<HTML>
  <HEAD>
      <title>WebForm1</title>
  </HEAD>
  <body>
      <form name="Form1" method="post" action="test.php" id="Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="" />

<script language="javascript">
<!--
  function __doPostBack(eventTarget, eventArgument) {
      var theform;
      if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
          theform = document.forms["Form1"];
      }
      else {
          theform = document.Form1;
      }
      theform.__EVENTTARGET.value = eventTarget;
      theform.__EVENTARGUMENT.value = eventArgument;
      theform.submit();
  }
// -->
</script>

<textarea name="TextArea1" id="TextArea1" cols="60"><?=$WebForm1->ViewState['TextArea1']?></textarea><br/>

<input type="text" name="TextBox1" id="TextBox1" value="<?=$WebForm1->ViewState['TextBox1']?>"/>
<br>
<input language="javascript" onclick="__doPostBack('Button2Click','')" name="Button2" id="Button2" type="button" value="提交" />
</form>
</body>
</HTML>
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个