开发者

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 e.g. 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 i开发者_如何学运维n 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜