我是用户控件的新手, 我有一种情况,我无法解决如下
单击用户control1中的按钮时,在同一页面上的用户control2中绑定一个下拉列表。和用户控件在他们自己的更新面板中。
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中的按钮,然后在用户控件1 shud b bound中下拉列表 请帮忙
提前致谢
答案 0 :(得分: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();
}
简而言之,页面处理对用户控件的引用,因此两个用户控件彼此不在页面上,或者每个用户控件都不存在。该页面还处理协调当用户控件引发事件并通过公共方法调用其他控件时发生的事情。