views:

41

answers:

3

I am relatively new to this, but here is my problem.

In asp.net, I have a parent and a child control. Within the child control I have a dropdown list. Based on dropdown list's selected value I would like to toggle Visibility of Panel in parent control. For instance if I select Show in child control dropdown list, I need to pass true to parent control to make Panel visible and vice versa. How should I do that. I have read that can be done via the event handling and have seen certain scenarios but I am not clear on that. Please help!

Thanks.

A: 

One way to do it is to expose the dropdown list (public) and in your parent control check the child controls dropdown to see if it should show or hide the panel on page load. If this works or not depends a little on the page lifecycle.

Another way to do it is to store the drop-down value in ViewState on the change event. That way the ViewState parameter can be read by the parent control.

If possible you should definitely go for the first option.

Merrimack
But another problem is when the value is changed in child control's dropdown list, the event is triggered after the Page_Load event of the parent control is fired, so parent control always finds ViewState value as null. This goes for both of your solutions. If SelectedIndexChanged for the child dropdown is fired after the parent's Page_Load I can't possibily read the values in parent...
Ashar Syed
Try checking the value of the drop-down in the "PreRender" event of your parent control. That should happen after the change event triggered on the child control.
Merrimack
A: 

Basically, you just need to subscribe to the SelectedIndexChanged event and handle it. The event is fired when the selected item was changed. Note that you should allow auto-postback on the drop down control to make sure the event is fired just after the user changed the drop down's value.

In the ASPX file:

<asp:DropDownList … OnSelectedIndexChanged="OnDropDownChanged">…</asp:dropDownList>

In case you are creating the control in the code-behind, subscribe after creating the control like this:

dropDown.SelectedIndexChanged += OnDropDownChanged;

And then handle it:

public void OnDropDownChanged(object sender, EventArgs e)
{
    // alter the panel's visibility here; the drop down's value contains
    // the selected item; note that you shoud use "(DropDownList)sender"
    // to access it
}

EDIT: Also, have a look on a more elaborate example on MSDN. Note that the event is declared in DropDownList's ancestor 'ListControl'.

Ondrej Tucny
Ondrej, that's what I tried. On IndexChanged I am setting ViewState["HideSubmit"] = 1. In parent control's Page_Load I am fetching the ViewState's value but its null.
Ashar Syed
As what I noticed when I change value in dropdown, the very first event that is fired in parent's control Page_Load. And within this, it find's ViewState["HideSubmit"] null because SelectedIndexChanged event is not yet fired and the ViewState's value has not yet been set. It is after the parent's Page_Load that it executes SelectedIndexChanged of the dropdown in child control.
Ashar Syed
@Ashar Syed viewstate is **local** to a control. In the parent control subscribe to the child control's event _or_ put the code into the surrounding ASPX code — whatever fits your actual design.
Ondrej Tucny
How should I subscribe to the child control's event. Would you please provide an example. Thanks.
Ashar Syed
@Ashar I just did that in my answer and provided you with two methods.
Ondrej Tucny
+2  A: 

Raise an event that your parent control listens for.

In the code behind for your parent control, create an object of the type of your child control. Something like:

private MyWebControl childControl;

Then in the child control, define an event

public event System.EventHandler SelectionChanged;

In the OnIndexChanged event of your DropDownList, after you do your processing, raise your event:

if(this.SelectionChanged!= null)
{
     this.SelectionChanged(this, new EventArgs()); 
     // You can send the index of the DDL in the event args
}

In your parent control, wire up the event. Page_Init is good

this.childControl.SelectionChanged+=new EventHandler(childControl_SelectionChanged);

Still in the parent control, define your method

private void childControl_SelectionChanged(object sender, EventArgs e)
{
      /// Do your processing here.
      /// Grab the DDL's index from the EventArgs and do your processing

}

Should be all you need to get it working!

TheGeekYouNeed
Hey thanks! That worked :). After the solution you provided, With a little modification I made that worked. Thanks alot for your help. I really do appreciate it!
Ashar Syed
You're welcome! Glad I could help :)
TheGeekYouNeed