开发者

Sharing value from child to parent control

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 li开发者_运维知识库st, 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.


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!


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.


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'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜