我刚刚为我的MVC框架构建了一个文本框控件,在完成整个文档之前,我对从servertag继承的所有内容调用prerender(而servertag又从domelement继承)。
我发现更改domElement派生对象的标记名的唯一方法是用一个新的标记名替换它,所有属性都与旧的标记名同步。
这就是代码中的情况:
protected function PreRenderTextarea( WebPage $sender )
{
$textarea = $sender->createElement( 'textarea' );
foreach( $this->attributes as $attribute )
{
if ( $attribute->name == 'value' )
{
$textarea->nodeValue = $attribute->value;
}
else
{
$textarea->setAttribute( $attribute->name, $attribute->value );
}
}
$this->parentNode->replaceChild( $textarea, $this );
}
public function OnPreRender( WebPage $sender )
{
parent::OnPreRender();
$this->setAttribute( 'value', $this->Value );
switch( $this->Mode )
{
case 'normal' :
$this->setAttribute( 'type', 'text' );
break;
case 'password' :
$this->setAttribute( 'type', 'password' );
break;
case 'multiline' :
$this->PreRenderTextarea( $sender );
return;
break;
}
}
这真的是唯一的方法吗?这种方式有一个相当不必要的副作用,那就是取消控制背后的所有逻辑。