在一個使用者控制項內操作另外一個使用者控制項的中的控制項

來源:互聯網
上載者:User

比如一個論壇,上面一個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改變了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.