我有一个 GrandParent 用户控件 top.asxc。在里面我有一个 radSplitter 控件,它将页面分成 2 个部分。在左侧部分,我加载了 Left.ascx 控件,该控件内部有一个 radTreeView,右侧是 right.ascx。在 right.ascx 控件上,我有一个按钮,当我单击它时,我想对 left.ascx 控件上的 radTreeView 控件进行数据绑定。这是一种方法吗?
问问题
551 次
1 回答
0
您需要将事件从 right.ascxUserControl
传递到 top.ascx UserControl
。然后top.ascxUserControl
会触发你想要的事件,因为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();
}
}
于 2012-11-02T14:08:38.690 回答