问题来自论坛/u/0415/17/3f264265-b25c-4db8-a192-520e8a60e4c1.html?85396
问题分析,aspx页面的控件需要控制显示与否,就如同象是一个小电灯,为了不让它通电之后常亮。因此需要一个开关才能控制到它。什么样的开关它管不了那么多,只要能控制开与关功能即可。
用户控件的Button,它可以实现开关功能。它可以控制电器的电路开与关。
接下来,两个电器是不同的对象,怎样让它们连接在一起。在程序中,可以使用Interface(接口)来实现。我们可以写一个叫开关接口ISwitchable。
ISwitchableusingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
///<summary>
///SummarydescriptionforISwitchable
///</summary>
{
publicinterfaceISwitchable
{
voidSwitch(boolshow);
}
}
是什么物件需要控制,也就是说什么电器需要安装开关,这里是aspx的控件需要控制显示与否。因此aspx.cs实现这个接口。
View Code usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
;
publicpartialclass_Default:System.Web.UI.Page,ISwitchable
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
publicvoidSwitch(boolshow)
{
this.TextBox1.Visible=show;
}
}
接下来,用户控件实现代码:
View Code usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
;
publicpartialclassInsusWebUserControl:System.Web.UI.UserControl
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
Buttonbtn=(Button)sender;
ISwitchablesw=(ISwitchable)this.Page;
switch(btn.Text)
{
case"开":
btn.Text="关";
sw.Switch(true);
break;
case"关":
btn.Text="开";
sw.Switch(false);
break;
}
}
}
运行效果:
源程序(.NET3.5 + + C#):
/insus/ASPDOTNET/uc_ctrl_ctrlOfpage.zip