比如一個論壇,上面一個top使用者控制項用來登陸,下面一個buttom的使用者控制項用來顯示線上人數,要求登陸以後立即重新整理online中的線上人數,就需要在一個使用者控制項中操作另外一個使用者控制項中的控制項(這2個使用者控制項都包含在一個頁面中)
看過
http://www.cnblogs.com/lovecherry/archive/2005/03/25/125515.html
和
http://www.cnblogs.com/lovecherry/archive/2005/04/11/135543.html
的人應該馬上就能知道怎麼做,其實就是2者的結合。
比如建立2個使用者控制項WebUserControl1.ascx和WebUserControl2.ascx
後者放置一個Label(public System.Web.UI.WebControls.Label online)
2個使用者控制項拖放到頁面中去,指定id:
<uc1:WebUserControl1 id="top" runat="server"></uc1:WebUserControl1>
<uc1:WebUserControl2 id="buttom" runat="server"></uc1:WebUserControl2>
前者內放置一個按鈕,按鈕的單擊事件如下:
((WebUserControl2)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).FindControl("buttom")).online.Text="已經更新";
//首先是鎖定到這個頁面(System.Web.UI.Page)System.Web.HttpContext.Current.Handler
//然後從頁面鎖定到這個使用者控制項(WebUserControl2)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).FindControl("buttom")
//最後從這個使用者控制項鎖定到使用者控制項內部的控制項((WebUserControl2)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).FindControl("buttom")).online
測試一下,按下第一個使用者控制項中的按鈕,第二個使用者控制項的Label改變了。