我有兩個用戶控件,
WebUserControl1.ascx
WebUserControl2.ascx,其中WebUserControl1.ascx 拖放在WebUserControl2.ascx內。
在test。aspx中加載WebUserControl2.ascx控件。
現在想WebUserControl1.ascx調用父容器WebUserControl2.ascx控件的一個方法。
請問怎么實現呢?
我實現的代碼:
WebUserControl1.ascx
public event EventHandler DisplayData;
protected void btnComp_Click(object sender, EventArgs e)
    {
       if (DisplayData != null)//沒有執行該方法,DisplayData  為空,很郁悶
            DisplayData(this, EventArgs.Empty);
}
==========
WebUserControl2.ascx
WebUserControl1-1.DisplayData += InitMulti;
   public void InitMulti(Object sender,EventArgs e)
    {
        
    }
代碼來實現,結果發現。
DisplayData  為空,很郁悶
如果,我將
WebUserControl1.ascx 不放在WebUserControl2.ascx內,
兩個用戶控件都放在test.aspx中,上述代碼沒有問題,
各位大俠,救救我吧。上面的代碼,剛好是參考saucer的

解决方案 »

  1.   

    WebUserControl2.ascx.cs:
    protected WebUserControl1 wuc1;
    注册事件
    wuc1.RefreshData += new TjRealMS2006.WebUI.Common.SplitPageIconModule.EventHandler(ModuleSplitPageIcon_RefreshData);private void ModuleSplitPageIcon_RefreshData(object sender, EventArgs e)
    {
    ...
    }WebUserControl1.ascx.cs:
    public delegate void EventHandler(object sender, System.EventArgs e);
    EventHandler m_EventHandler;
    public event EventHandler RefreshData
    {
    add{m_EventHandler += value;}
    remove
    {m_EventHandler -= value;}
    }
      

  2.   

    //子控件:
    //前台页面:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="testWeb.WebUserControl1" %>
    <asp:Button ID="btnCall" runat="server" Text="调用父控件方法" OnClick="Button1_Click" />&nbsp;
    <asp:Label ID="Label1" runat="server" Text="子控件"></asp:Label>
    cs:
    protected void Button1_Click(object sender, EventArgs e)
            {
                WebUserControl2 ctl = this.Parent as WebUserControl2;
                if (ctl != null)
                    ctl.btnShowMsg_Click(null, null);
            }
    //父控件
    //前台:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="testWeb.WebUserControl2" %>
    <%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
    <asp:Label ID="Label1" runat="server" Text="父控件" Width="120px"></asp:Label>
    <asp:Button ID="btnShowMsg" runat="server" OnClick="btnShowMsg_Click" Text="显示信息" /><br />
    <uc1:WebUserControl1 ID="WebUserControl1_1" runat="server" />//cs:
      public void btnShowMsg_Click(object sender, EventArgs e)
            {
                this.Page.Response.Write("<script>alert('哈哈')</script>");
            }这种直接的方法能实现楼主需求吗?
      

  3.   

    用Parent对象如:
    Web.. ascx2 = Parent as Webascx2;
    if(ascx2 != null){
      ascx2// 操作
    }
      

  4.   

    <uc1:WebUserControl1 ID="WebUserControl1_1" runat="server" DisplayData="InitMulti" />
      

  5.   

    我来看看,以上代码是否可行。to # LikeCode ,能否将你的代码贴出来让我们学习呢。昨天晚上,我还尝试
    将每个用户控件当作一个类
    再新建一个实体,负责两个类的交互。
    但是。没有成功,vs2005 的用户控件,类名是什么呢,我new不了用户控件,郁闷。
      

  6.   

    不知道为什么,我最上贴的代码,在子用户控件的 DisplayData 会为空,奇怪。
      

  7.   

    暈,我還在,兄弟。wjjybx 
      

  8.   

    反射方法,演示在用户控件里调用页面上的一个方法名为SetLabelText的方法
    用户控件名:WebUserControl.ascx
    ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="invoke method of page" />.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Reflection;public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Web.UI.Page page = this.Page;
            Type pageType = page.GetType();
            MethodInfo methodInfo = pageType.GetMethod("SetLabelText");
            object[] paras = new object[] { "set Label1.Text By UserControl" };
            methodInfo.Invoke(page, paras);
        }
    }
      

  9.   

    不知道你想要干什么,也许设计上有问题,但按你的思路做,应该可以的,也许你没装载正确,参考1. WebUserControl1.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %><div style="color:red">
    Inside WebUserControl1:
    <asp:Button ID="btnComp" runat="server" Text="Click Me" OnClick="btnComp_Click" />
    </div>2. WebUserControl1.ascx.csusing System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
        public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            public event EventHandler DisplayData;         protected void Page_Load(object sender, EventArgs e)
            {        }        protected void btnComp_Click(object sender, EventArgs e)
            {
                if (DisplayData != null)
                    DisplayData(sender, EventArgs.Empty);
            }
        }
    }
    3. WebUserControl1.ascx.designer.csnamespace WebApplication1 {
            public partial class WebUserControl1 {        protected global::System.Web.UI.WebControls.Button btnComp;
        }
    }4. WebUserControl2.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
    <%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
    <uc1:WebUserControl1 id="WebUserControl1_1" runat="server">
    </uc1:WebUserControl1>5. WebUserControl2.ascx.csusing System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
        public partial class WebUserControl2 : System.Web.UI.UserControl
        {
            protected void Page_Init(object sender, EventArgs e)
            {
                WebUserControl1_1.DisplayData += InitMulti;
            }
            protected void Page_Load(object sender, EventArgs e)
            {
            }        public void InitMulti(Object sender, EventArgs e)
            {
                Page.Response.Write("Inside WebUserControl2, this is triggered by WebUserControl1");
            }     }
    }6. WebUserControl2.ascx.designer.csnamespace WebApplication1 {
        
        public partial class WebUserControl2 {
            
            protected global::WebApplication1.WebUserControl1 WebUserControl1_1;
        }
    }
    7. Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        </div>
        </form>
    </body>
    </html>8. Default.aspx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Control c = LoadControl("WebUserControl2.ascx");
                form1.Controls.Add(c);
            }
        }
    }9. Default.aspx.designer.cs
    namespace WebApplication1 {
        
        
        public partial class _Default {
            
            protected global::System.Web.UI.HtmlControls.HtmlForm form1;
        }
    }
      

  10.   

    LZ 问题解决了么?要不看看这个,把WebUserControl.ascx中的button的click事件放到它(WebUserControl.ascx)的父容器中定义了
    用户控件   
    WebUserControl.ascx
       <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Ascx_WebUserControl" %>
    <asp:Button ID="Button1" runat="server" Text="Button"  />
    Default.aspx.cs   
         
            Button btn=(Button)this.WebUserControl1.FindControl("Button1");
            btn.Click   +=new System.EventHandler(this.Button_Click);  
        
        private void Button_Click(object sender, System.EventArgs e)
        {
            ((Button)sender).Text += " | abc";
        }
    Default.aspx <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ Register Src="Ascx/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="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 onload="">
        <form id="form1" runat="server">
            <uc1:WebUserControl ID="WebUserControl1" runat="server" />        
        </form>
    </body>
    </html>
      

  11.   

    要绑定控件事件,要写在Init 里面,呵呵,或者在前台写