页面page_A里使用了用户控件uc_A,用户控件uc_A中嵌套了用户控件uc_B,当触发了用户控件uc_B中的事件后需要调用uc_A中的方法以更新uc_A的内容.我尝试在uc_B中使用这样的方法来达到目的:protected void uc_B_Event(param param1)
    {
        ... ...
        Page p = this.Parent.Page;
        UserControl uc = p.FindControl(uc_A) as UserControl;
        Type t = uc.GetType();
        MethodInfo mi = t.GetMethod(uc_A_Function);
        if (mi != null)
        {
            mi.Invoke(uc, new object[] {});
        }
    } 错误提示:"System.NullReferenceException: 未将对象引用设置到对象的实例"
错误提示行:Type t = uc.GetType();如何在嵌套用户控件中互相调用其方法呢?

解决方案 »

  1.   

    给你写个例子:
    UserControl1:<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
    <%@ Register Src="~/WebUserControl2.ascx" TagPrefix="my" TagName="uc2" %><my:uc2 runat="server" ID="UC2" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        public void SetText(string str)
            {
                this.TextBox1.Text = str;
            }
        }
    UserControl2:<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />    public partial class WebUserControl2 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        protected void Button1_Click(object sender, EventArgs e)
            {
                if (this.Parent is UserControl)
                {
                    System.Reflection.MethodInfo mi = this.Parent.GetType().GetMethod("SetText", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    if (mi != null)
                    {
                        mi.Invoke(this.Parent, new object[] { "Hello" });
                    }
                }
            }
        }
    调用页:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="flash.aspx.cs" Inherits="WebApplication1.flash" %>
    <%@ Register Src="~/WebUserControl1.ascx" TagPrefix="my" TagName="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <my:uc1 ID="UC1" runat="server"></my:uc1>
        </div>
        </form>
    </body>
    </html>
      

  2.   

    以上写法是针对Web Site项目,如果你的项目是WebApplication,那就更简单了:        protected void Button1_Click(object sender, EventArgs e)
            {
                if (this.Parent is WebUserControl1)
                {
                    WebUserControl1 wuc = this.Parent as WebUserControl1;
                    wuc.SetText("Hello");
                }
            }
      

  3.   

    但是我在uc_B中查看过this.Parent的值,是调用uc_A的页面page_A阿,并不是uc_A.