2013-11-21 68 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(); 
} 

總之,頁面處理對用戶控件的引用,所以兩個用戶控件彼此都不在頁面上,或者其他每個控件都存在。該頁面還處理協調當用戶控件引發事件並通過公共方法調用另一個控件時發生的情況。