当前位置: 移动技术网 > IT编程>开发语言>.net > 用户控件如何控制ASPX页面的控件

用户控件如何控制ASPX页面的控件

2018年09月16日  | 移动技术网IT编程  | 我要评论

最新手机铃声排行榜,商都体育,地磅误差

问题分析,x页面的控件需要控制显示与否,就如同象是一个小电灯,为了不让它通电之后常亮。因此需要一个开关才能控制到它。什么样的开关它管不了那么多,只要能控制开与关功能即可。
用户控件的button,它可以实现开关功能。它可以控制电器的电路开与关。
接下来,两个电器是不同的对象,怎样让它们连接在一起。在程序中,可以使用interface(接口)来实现。我们可以写一个叫开关接口iswitchable。
  iswitchable
using system;
using system.collections.generic;
using system.linq;
using system.web;

/// <summary>
/// summary description for iswitchable
/// </summary>
namespace insus.net
{
    public interface iswitchable
    {
        void switch(bool show);
    }
}

 
是什么物件需要控制,也就是说什么电器需要安装开关,这里是aspx的控件需要控制显示与否。因此aspx.cs实现这个接口。
  view code
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using insus.net;

public partial class _default : system.web.ui.page,iswitchable
{
    protected void page_load(object sender, eventargs e)
    {
      
         
    }

    public void switch(bool show)
    {
        this.textbox1.visible = show;
    }
}

 
接下来,用户控件实现代码:
  view code
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using insus.net;

public partial class insuswebusercontrol : system.web.ui.usercontrol
{
    protected void page_load(object sender, eventargs e)
    {

    }
    protected void button1_click(object sender, eventargs e)
    {
        button btn = (button)sender;
        iswitchable sw = (iswitchable)this.page;

        switch (btn.text)
        {
            case "开":
                btn.text = "关";
                sw.switch(true);
                break;
            case "关":
                btn.text = "开";
                sw.switch(false);
                break;
        }
    }
}

 
运行效果:
\


源程序(.net3.5 + asp.net + c#):

 


摘自 insus.net

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网