• 如何在一个用户控件内操作另外一个用户控件的中的控件? (from LoveCherry)


    比如一个论坛,上面一个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改变了。

  • 相关阅读:
    Bzoj1101 [POI2007]Zap
    Bzoj2393 Cirno的完美算数教室
    UVa10891 Game of Sum
    Bzoj4128 Matrix
    类的组合
    继承
    属性查找与绑定方法
    类与对象
    面向对象程序设计——基础概念
    修改个人信息的程序
  • 原文地址:https://www.cnblogs.com/gxh973121/p/135995.html
Copyright © 2020-2023  润新知