C# – Find Usercontrol’s Dropdownlist selected value on Page

asp.netc

I have a User Control named UCCountry.ascx, this user control only has one dropdownlist of country. The name of my Page is MyFirstPage.aspx, on this page I have a repeater that binds the data based on the CountryId i.e MyfirstPage.aspx contains the Usercontrol.
Now I want to know how I can bind the data when a user changes the country using the dropdownlist in the Usercontrol.

Note: Which page life cycle event can I use to get the value of the dropdownlist and on which event do I need to bind the repeater.

Best Answer

You'll need to expose the DropDownList as public in your control. That way your page can listen for events and respond to them. Eg:

UCCountry.ascx

<asp:DropDownList Id="CountryDropDown" ... />

UCCountry.ascx.cs

public DropDownList CountryDropDown { get; protected set; }

MyFirstPage.aspx

<ctl:UCCountry Id="CountryControl" ... />

MyFirstPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    this.CountryControl.CountryDropDown.SelectedIndexChanged += new EventHandler(CountryDropDown_SelectedIndexChanged);
}

protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
    // do your databinding here
}