我这里有一个aspx页面,里面有两个用户控件(a.ascx和b.ascx),其中a.ascx中包含一个gridview控件,现在我想在b.ascx中找到a.ascx中的gridview控件并给它的datasource赋值……
请问为什么这样获得的sc是null                SmallClass sc = new SmallClass();
                GridView gv = (GridView)sc.FindControl("SmallClassGV");应该怎么办啊……

解决方案 »

  1.   

    设置属性
    public GridView gv
    {
    get {return this.GridView1;}
    }
      

  2.   

    参考
    http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2008/09/03/how-to-access-one-usercontrol-from-another-usercontrol-using-asp-net.aspx
      

  3.   

    GridView g = GridView1.Page.FindControl("WebUserControl2").FindControl("GridView2") as GridView;
      

  4.   

    我在a a1 = new a()时,a1是null……这是为什么……
      

  5.   

    解决了,这里还有一种方法……GridView gv = this.Parent.Parent.FindControl("a").FindControl("gridview1") as GridView;
    在b.ascx.cs中写a a1 = new a(),a1为空,是因为用户控件a并不在b中,而是同级的……