简体   繁体   中英

Call an event handler in a user control from host page

I have a dropdownlist in a user control and i want to perform different functions on the basis of the host page that hosts this user control when the selected index of the dropdownlist changes eg when the host page is locations i want the event to load locations for the selected item in the dropdown, when the hosting page is services i want to load services for the selected item in the dropdown. What is the best possible way to achieve this scenario.

Is the below helpful for you?

internal void BindDDL(DropDownList ddl, Object source, string dataValueField, string dataTextField)
    {
        ddl.DataValueField = dataValueField;
        ddl.DataTextField = dataTextField;
        ddl.DataSource = source;
        ddl.DataBind();
    }

Whenever you want to notify something to a parent control, a good practice is to raise an event from a child control to which parent control registers and does what is needed.

On the other hand, it is a bad practice to pass a reference of parent control to child control so that child can invoke a method on parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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