我有一个GrandParent用户控件top.asxc。在里面我有一个radSplitter控件,它将页面分为2个部分。在左侧部分,我在Left.ascx控件内部加载,其内部有一个radTreeView,右侧是right.ascx。在right.ascx控件上我有一个按钮,当我点击它我想要数据绑定在left.ascx控件上的radTreeView控件。这是一种方法吗?
答案 0 :(得分:0)
您需要将该事件从right.ascx UserControl
传递到top.ascx UserControl
。然后top.ascx UserControl
将触发您想要的事件,因为top.ascx确实有left.ascx的实例。这是如何......
在right.ascx.cs文件中:
public event EventHandler RightButton_Clicked;
protected void RightButton_Click(object sender, EventArgs e)
{
EventHandler handler = RightButton_Clicked;
if (handler != null)
{
handler(this, new EventArgs());
}
}
在top.ascx.cs文件中:
private static Control _leftUC;
private static Control _rightUC;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitWebUserControls();
}
// Note: if your UserControl is defined in the ascx Page, use that definition
// instead of the dynamically loaded control `_right_UC`. Same for `_leftUC`
private void InitWebUserControls()
{
_leftUC = Page.LoadControl("left.ascx");
_rightUC = Page.LoadControl("right.ascx");
((right)_rightUC).RightButton_Clicked += new EventHandler(RightUC_Event);
}
void RightUC_Event(object sender, EventArgs e)
{
((left)_leftUC ).UpdateControl();
}
在left.ascx文件中:
public void UpdateControl()
{
leftRadTreeView.Rebind();
}
如果您最终想要将事件参数传递给top.ascx UserControl
,而不是使用new EventArgs()
,请使用实现EventArgs
的自定义类并以这种方式使用它:
int myArgument = 1;
handler(this, new RightEventArgs(myArgument));
public class RightEventArgs: EventArgs
{
public int argumentId;
public RightEventArgs(int selectedValue)
{
argumentId = selectedValue;
}
}
然后您可以通过验证其EventArgs
具体类型来过滤传递给top.ascx的事件:
private void InitWebUserControls()
{
_leftUC = Page.LoadControl("left.ascx");
((left)_leftUC ).LeftButton_Clicked += new EventHandler(Child_Event);
_rightUC = Page.LoadControl("right.ascx");
((right)_rightUC).RightButton_Clicked += new EventHandler(Child_Event);
}
void Child_Event(object sender, EventArgs e)
{
if( e is RightEventArgs)
{
((left)_leftUC).UpdateControl();
}
else if (e is LeftEventArgs)
{
((right)_rightUC).ClearSelection();
}
}