[英]Passing value from One user control to another usercontrol
I have three usercontrols uc1.ascx ,uc2.ascx ,UC_Combined.ascx 我有三个用户控件uc1.ascx,uc2.ascx,UC_Combined.ascx
UC1 has one label control UC1具有一个标签控件
UC2 has one Dropdownlist Control UC2具有一个Dropdownlist控件
UC_Combined is created by combining UC1 and UC2 UC_Combined是通过组合UC1和UC2创建的
Now I placed UC_Combined.ascx on my aspx page webForm1.aspx has one more Label servercontrol 现在我将UC_Combined.ascx放置在我的aspx页面上webForm1.aspx还有一个Label servercontrol
Now when I run my webForm1.aspx page I can see see DropDown list and a Label 现在,当我运行我的webForm1.aspx页面时,可以看到DropDown列表和Label
Now when I select an Item from dropdown list ,I want the value of the selection to display to the Label 现在,当我从下拉列表中选择一个项目时,我希望选择的值显示在标签上
Can some one suggest me how can I do this . 有人可以建议我该怎么做。
It's not best to create a dependency between parent and child controls. 最好不要在父控件和子控件之间创建依赖关系。 Something you should generally avoid.
通常应该避免的事情。 But, if you have to do it or in some way makes your life alot easier then there are a few techniques for achieving this while keeping the controls somewhat decoupled.
但是,如果您必须这样做或以某种方式使您的生活更加轻松,那么可以通过一些技巧来实现此目的,同时保持控件之间的某种程度的分离。 I would suggest you do the following:
我建议您执行以下操作:
Think you got it? 想你明白了吗?
Easy. 简单。 Implement a event on the uc containing the drop down like:
在uc上实现包含下拉列表的事件,如下所示:
public event EventHandler<DDSelectionChangedEventArgs> DDSelectionChanged;
public virtual void OnDDSelectionChanged(DDSelectionChangedEventArgs e)
{
if (DDSelectionChanged != null)
{
DDSelectionChanged(this, e);
}
}
The selection changed handler of the dd then have to call OnDDSelectionChanged. 然后,dd的选择更改处理程序必须调用OnDDSelectionChanged。
Register a handler onto that event in your page (aspx). 将处理程序注册到页面(aspx)中的该事件上。 This handler should then call something like ChangeText(text) on the second uc with the textbox.
然后,此处理程序应使用文本框在第二个uc上调用类似ChangeText(text)之类的东西。 And the textbox is been updated.
并且文本框已更新。
So the communication between the uc's is driven by events and the page has the resposibility to wire the events up. 因此,uc之间的通信受事件驱动,页面具有将事件连接起来的责任。 The uc's are completely independent.
uc是完全独立的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.