2013-11-21 41 views
1
同一页

上的另一个用户控件我是新来的用户控件加载下降一个用户控制下来, 我有我无法解决像下面

当在用户控制1按钮按下的情况在同一页面上的用户控件2中绑定一个下拉列表。和用户控件都在他们自己的更新面板中。

的UserControl1 ASCX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="SampleWebApp.WebUserControl1" %> 
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> 

UserControl2 ASCX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SampleWebApp.WebUserControl2" %> 
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 

以上两个用户控件是一个aspx页面像下面 ASPX

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
    <div> 
    <UC2:UserControl1 ID="UserControl1" runat="server" /> 
    </div> 
    </ContentTemplate> 
    </asp:UpdatePanel> 
    <asp:UpdatePanel ID="UpdatePanel2" runat="server"> 
    <ContentTemplate> 
    <div> 
<UC2:UserControl2 ID="usercontrol2" runat="server" /> 
    </div> 
    </ContentTemplate> 
    </asp:UpdatePanel> 

现在最大的问题是 时我点击用户控件2中的按钮,然后下载lis吨的用户控制1 shud b势必 请帮

在此先感谢

回答

0

我建议每个用户设置的事件控制着承载用户控件的页面订阅,然后有逻辑页面事件处理程序,知道要告诉通过用户的控制,他们需要重新绑定方法/属性的用户控件,就像这样:

public class UserControlClass1 
{ 
    // Define event that will be raised by user control to anyone interested in handling the event 
    public event UC_Button1ClickEventHandler UC_Button1Click; 
    public delegate void UC_Button1ClickEventHandler(); 

    public void DoSomething1() 
    { 
     // This method can do anything you want the control to do (i.e. rebind, etc.) 
    } 

    // Mechanism to allow event to be raised by user control 
    private void Button1_Click(System.Object sender, System.EventArgs e) 
    { 
     if (UC_Button1Click != null) 
     { 
      UC_Button1Click(); 
     } 
    } 
} 

public class UserControlClass2 
{ 
    // Define event that will be raised by user control to anyone interested in handling the event 
    public event UC_Button2ClickEventHandler UC_Button2Click; 
    public delegate void UC_Button2ClickEventHandler(); 

    public void DoSomething2() 
    { 
     // This method can do anything you want the control to do (i.e. rebind, etc.) 
    } 

    // Mechanism to allow event to be raised by user control 
    private void Button2_Click(System.Object sender, System.EventArgs e) 
    { 
     if (UC_Button2Click != null) 
     { 
      UC_Button2Click(); 
     } 
    } 
} 

现在承载两个用户控件的.aspx页,它可以订阅每个用户控制事件,如下所示:

userControl1.UC_Button1Click += Button1_Click; 
userControl2.UC_Button2Click += Button2_Click; 

在这里,在事件处理程序的实现是在页面管理两个用户控件之间的互动,就像这样:

// When the button in user control 1 is clicked, then the DoSomething2 method 
// is called in user control 2 (i.e. bind data, etc.) 
public void Button1_Click(object sender, EventArgs args) 
{ 
    // Call the DoSometing2 method in user control 2 
    userControl2.DoSomething2(); 
} 

// When the button in user control 2 is clicked, then the DoSomething1 method 
// is called in user control 1 (i.e. bind data, etc.) 
public void Button2_Click(object sender, EventArgs args) 
{ 
    // Call the DoSometing1 method in user control 1 
    userControl1.DoSomething1(); 
} 

总之,页面处理对用户控件的引用,所以两个用户控件彼此都不在页面上,或者其他每个控件都存在。该页面还处理协调当用户控件引发事件并通过公共方法调用另一个控件时发生的情况。