should I make a separate class of controls that are used in different aspx pages
Say I have a drop down list i开发者_运维问答n asp.net webforms that gets filled with the same data everytime for example.
someDataContext db = new someDataContext();
int ID = 1;
var randomItems = db.table.Where(x => x.ID == ID);
foreach(var randomItem in randomItems) {
dropDownList.Items.Add(new ListItem(randomItem.Name, randomItem.ID.ToString());
}
If I am using this code across multiple aspx pages should I put this control into a class so don't violate the DRY principle?
yes I would do it so in case you decide to change from DropDown to another control you only have to touch one place and all pages will either break or work, depends on how good is your encapsulation :)
do one thing create one User Control of this drop down, you can you that user control anywhere just with drag and drop
精彩评论