I am doing a simple page (Default.aspx), where is a DropDownList (id colors) control. It is easy to populate it with items in PageLoad method.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
colors.Items.Add("red");
}
} etc. ....
However, is it possible to fill the colors control from external class file (I mean some class file located in AppCode folder).
Thanks.
Absolutely. For example, if you just want to create some generic utility method to bind a
IEnumerable<T>of values to aDropDownList, you’d pass both of those object as arguments to the method:What makes this work is the fact that classes like
DropDownListare reference types. This means that the instance ofdropDownin the method is not a copy of the one form the page, but is instead the same one from the page. So modifications made to it in this method will be applicable to theDropDownListon the page itself.So to call it from the page, you’d do something like:
Where
colorValuesis some list of values to bind to the list. (AndHttpUtilsis the name of the class containing the above method, but you can name it whatever you want.)