C# 在2个动态加载的用户控件之间传递数据

C# 在2个动态加载的用户控件之间传递数据,c#,user-controls,C#,User Controls,我有一个场景,在一个webform中动态加载2个用户控件,并需要它们一起交互。代码如下: default.aspx.cs ControlA cA = (ControlA)LoadControl("~/controlA.ascx"); ControlB cB = (ControlB)LoadControl("~/controlB.ascx"); Page.Controls.Add(cA); Page.Controls.Add(cB); 现在的问题是,controlB需要将数据传递给control

我有一个场景,在一个webform中动态加载2个用户控件,并需要它们一起交互。代码如下:

default.aspx.cs
ControlA cA = (ControlA)LoadControl("~/controlA.ascx");
ControlB cB = (ControlB)LoadControl("~/controlB.ascx");
Page.Controls.Add(cA);
Page.Controls.Add(cB);
现在的问题是,controlB需要将数据传递给controlA。数据不是来自事件(井的一部分)。当用户单击controlB中的链接时,我们从数据库中获取数据,我需要传递给controlA的是从请求中获取的第一个数据的ID。当然,当用户单击controlB中的链接时,page会进行回发

我查看了活动和代表,但我无法使其发挥作用。走这条路对吗


如果有人能提供线索,我将不胜感激

想到的一个快速而肮脏的方法是将第一次数据获取的ID声明为ControlB的公共属性,并在ControlB上的链接回发处理程序上设置该值 如。 在ControlB的事件处理程序上:

public void Link_click(object sender, EventArgs e)
{
 this.FirstFetchedId = GetValueFromDB();
}
然后在ControlA的预渲染上:

protected override void OnPreRender(EventArgs e)
{
ControlB cB = this.Page.FindControl("ControlBId") as ControlB; // either by ID or loop trough controls to find it by type
var YourIdIsHere = cB.FirstFetchedId;
base.OnPreRender(e);
}

想到的一个快速而肮脏的方法是将第一次数据获取的ID声明为ControlB的公共属性,并在ControlB上的链接回发处理程序上设置该值 如。 在ControlB的事件处理程序上:

public void Link_click(object sender, EventArgs e)
{
 this.FirstFetchedId = GetValueFromDB();
}
然后在ControlA的预渲染上:

protected override void OnPreRender(EventArgs e)
{
ControlB cB = this.Page.FindControl("ControlBId") as ControlB; // either by ID or loop trough controls to find it by type
var YourIdIsHere = cB.FirstFetchedId;
base.OnPreRender(e);
}