views:

114

answers:

2

I have three usercontrols uc1.ascx ,uc2.ascx ,UC_Combined.ascx

UC1 has one label control

UC2 has one Dropdownlist Control

UC_Combined is created by combining UC1 and UC2

Now I placed UC_Combined.ascx on my aspx page webForm1.aspx has one more Label servercontrol

Now when I run my webForm1.aspx page I can see see DropDown list and a 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 .

+1  A: 

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). The "Items" collection is a hashtable that has a lifespan of a single HTTP request. This means that it is cleared after the current HTTP request has been responded to. 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. 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. This way you can do everything within the same PostBack.

Think you got it?

peanutbutter_lou
Nice. +1. Making the best of a bad situation =)
RPM1984
A: 

Easy. Implement a event on the uc containing the drop down like:

    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.

Register a handler onto that event in your page (aspx). This handler should then call something like ChangeText(text) on the second uc with the textbox. 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. The uc's are completely independent.

Martin Kirsch