用户控件如何控制ASPX页面的控件
生活随笔
收集整理的這篇文章主要介紹了
用户控件如何控制ASPX页面的控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題來自論壇http://topic.csdn.net/u/20120415/17/3f264265-b25c-4db8-a192-520e8a60e4c1.html?85396
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);
????}
}
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;
????}
}
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;
????????}
????}
}
?
?問題分析,aspx頁面的控件需要控制顯示與否,就如同象是一個小電燈,為了不讓它通電之后常亮。因此需要一個開關才能控制到它。什么樣的開關它管不了那么多,只要能控制開與關功能即可。
用戶控件的Button,它可以實現開關功能。它可以控制電器的電路開與關。
接下來,兩個電器是不同的對象,怎樣讓它們連接在一起。在程序中,可以使用Interface(接口)來實現。我們可以寫一個叫開關接口ISwitchable。?
ISwitchableusing?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#):
http://download.cnblogs.com/insus/ASPDOTNET/uc_ctrl_ctrlOfpage.zip
?
轉載于:https://www.cnblogs.com/insus/archive/2012/04/16/2451664.html
總結
以上是生活随笔為你收集整理的用户控件如何控制ASPX页面的控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各种好用的代码生成器
- 下一篇: 开发人员必备:微软发布示例代码浏览器 (