简体   繁体   English

将值从一个用户控件传递到另一个用户控件

[英]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: 我建议您执行以下操作:

  1. Implement a PostBack handler that will store the value of the DropDownList in the "Items" collection of the HTTP Context (via HttpContext.Current.Items["ddlValue"] = val). 实现一个PostBack处理程序,该处理程序将DropDownList的值存储在HTTP上下文的“ Items”集合中(通过HttpContext.Current.Items [“ ddlValue”] = val)。 The "Items" collection is a hashtable that has a lifespan of a single HTTP request. “项目”集合是一个哈希表,其寿命为单个HTTP请求。 This means that it is cleared after the current HTTP request has been responded to. 这意味着在响应当前HTTP请求后将其清除。 It's a nice lightweight means of transporting data between components. 这是在组件之间传输数据的一种很好的轻量级方法。
  2. Implement a property in UC1 that lazy loads the value from the "Items" collection and reference the property in your markup with the <%= %> syntax. 在UC1中实现一个属性,该属性可以从“ Items”集合中延迟加载值,并使用<%=%>语法在标记中引用该属性。 Doing it this way ensures that you aren't trying to grab the value until Render (which is when the <%= %> code is executed), well after the PostBack handler event has been executed and the "Items" entry has been added. 这样,可以确保在执行PostBack处理程序事件和添加“ Items”条目之后,以及在Render(执行<%=%>代码时)之前,不尝试获取值。 。 This way you can do everything within the same PostBack. 这样,您可以在同一个PostBack中完成所有操作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM