I have few controls on my page which I want to show/hide and/or enable/disable on some condition.
Images to help illustrate the requirements:
screenshot1 screenshot2 screenshot3
Following is the code to show/hide, enable/disable the controls. Please review it and tell me how I can extract common code.
Any other suggestion are also more than welcome.
// Change controls status (enable/disable, show/hide) on the basis of selected item
protected void ddlLocationType_SelectedIndexChanged(object sender, EventArgs e)
{
int locationType = 0;
int.TryParse(ddlLocationType.SelectedItem.Value, out locationType);
EnableRelavantLocaitonControls(locationType);
}
private void EnableRelavantLocaitonControls(int locationType)
{
switch (locationType)
{
case 0:
DefaultControlsPosition();
break;
case 1:
EnableControlsForProvince();
break;
case 2:
EnableControlsForDistrict();
break;
case 3:
EnableControlsForTehsil();
break;
case 4:
EnableControlsForUC();
break;
case 6:
EnableControlsForVillage();
break;
default:
DefaultControlsPosition();
break;
}
}
// Show all drop downs.
// Hide all textboxes and disable all controls.
private void DefaultControlsPosition()
{
ddlProvince.Enabled = false;
ddlProvince.Visible = true;
txtProvince.Enabled = false;
txtProvince.Visible = false;
ddlDistrict.Enabled = false;
ddlDistrict.Visible = true;
ddlDistrict.Enabled = false;
txtDistrict.Visible = false;
ddlTehsil.Enabled = false;
ddlTehsil.Visible = true;
txtTehsil.Enabled = false;
txtTehsil.Visible = false;
ddlUC.Enabled = false;
ddlUC.Visible = true;
txtUC.Enabled = false;
txtUC.Visible = false;
ddlVillage.Enabled = false;
ddlVillage.Visible = true;
txtVillage.Enabled = false;
txtVillage.Visible = false;
}
// Show all drop downs except province drop down and disable all.
// Show and enable TextBox for province instead of drop down so user can
// enter name of province.
private void EnableControlsForProvince()
{
ddlProvince.Enabled = false;
ddlProvince.Visible = false;
txtProvince.Enabled = true;
txtProvince.Visible = true;
ddlDistrict.Enabled = false;
ddlDistrict.Visible = true;
ddlDistrict.Enabled = false;
txtDistrict.Visible = false;
ddlTehsil.Enabled = false;
ddlTehsil.Visible = true;
txtTehsil.Enabled = false;
txtTehsil.Visible = false;
ddlUC.Enabled = false;
ddlUC.Visible = true;
txtUC.Enabled = false;
txtUC.Visible = false;
ddlVillage.Enabled = false;
ddlVillage.Visible = true;
txtVillage.Enabled = false;
txtVillage.Visible = false;
}
// Show and Enable 'Province' drop down.
// Hide and Disable 'District' drop down.
// Show and Enable 'District' text box.
// Show and Disable all other drop downs beneath District.
private void EnableControlsForDistrict()
{
ddlProvince.Enabled = true;
ddlProvince.Visible = true;
txtProvince.Enabled = false;
txtProvince.Visible = false;
ddlDistrict.Enabled = false;
ddlDistrict.Visible = false;
txtDistrict.Enabled = true;
txtDistrict.Visible = true;
ddlTehsil.Enabled = false;
ddlTehsil.Visible = true;
txtTehsil.Enabled = false;
txtTehsil.Visible = false;
ddlUC.Enabled = false;
ddlUC.Visible = true;
txtUC.Enabled = false;
txtUC.Visible = false;
ddlVillage.Enabled = false;
ddlVillage.Visible = true;
txtVillage.Enabled = false;
txtVillage.Visible = false;
}
private void EnableControlsForTehsil()
{
ddlProvince.Enabled = true;
ddlProvince.Visible = true;
txtProvince.Enabled = false;
txtProvince.Visible = false;
ddlDistrict.Enabled = true;
ddlDistrict.Visible = true;
txtDistrict.Enabled = false;
txtDistrict.Visible = false;
ddlTehsil.Enabled = false;
ddlTehsil.Visible = false;
txtTehsil.Enabled = true;
txtTehsil.Visible = true;
ddlUC.Enabled = false;
ddlUC.Visible = true;
txtUC.Enabled = false;
txtUC.Visible = false;
ddlVillage.Enabled = false;
ddlVillage.Visible = true;
txtVillage.Enabled = false;
txtVillage.Visible = false;
}
private void EnableControlsForUC()
{
ddlProvince.Enabled = true;
ddlProvince.Visible = true;
txtProvince.Enabled = false;
txtProvince.Visible = false;
ddlDistrict.Enabled = true;
ddlDistrict.Visible = true;
txtDistrict.Enabled = false;
txtDistrict.Visible = false;
ddlTehsil.Enabled = true;
ddlTehsil.Visible = true;
txtTehsil.Enabled = false;
txtTehsil.Visible = false;
ddlUC.Enabled = false;
ddlUC.Visible = false;
txtUC.Enabled = true;
txtUC.Visible = true;
ddlVillage.Enabled = false;
ddlVillage.Visible = true;
txtVillage.Enabled = false;
txtVillage.Visible = false;
}
private void EnableControlsForVillage()
{
ddlProvince.Enabled = true;
ddlProvince.Visible = true;
txtProvince.Enabled = false;
txtProvince.Visible = false;
ddlDistrict.Enabled = true;
ddlDistrict.Visible = true;
txtDistrict.Enabled = false;
txtDistrict.Visible = false;
ddlTehsil.Enabled = true;
ddlTehsil.Visible = true;
txtTehsil.Enabled = false;
txtTehsil.Visible = false;
ddlUC.Enabled = true;
ddlUC.Visible = true;
txtUC.Enabled = false;
txtUC.Visible = false;
ddlVillage.Enabled = false;
ddlVillage.Visible = false;
txtVillage.Enabled = true;
txtVillage.Visible = true;
}
-
1\$\begingroup\$ You need to put your requirements in words here or a embedded picure... you can't expect us to click through your links just to figure out what you're doing... \$\endgroup\$Jeff Mercado– Jeff Mercado2012年07月11日 14:02:34 +00:00Commented Jul 11, 2012 at 14:02
2 Answers 2
First possible refactoring: define an enumeration for the three possible states
enum InputState{
Hidden,
Disabled,
Enabled
}
and use it with an extension method (or by extending the class)
public static void SetVisibility(this Control ctrl, InputState state){
switch(state){
case InputState.Hidden:
ctrl.Enabled = false;
ctrl.Visible = false;
break;
case InputState.Disabled:
ctrl.Enabled = false;
ctrl.Visible = true;
break;
case InputState.Enabled:
ctrl.Enabled = true;
ctrl.Visible = true;
break;
}
}
Start by creating a list of objects which contain the connection between a location type and the relevant controls. First create a new class to hold this information:
public enum LocationType { Undefined = 0, Province = 1, District = 2, ... } public class LocationControl { public LocationType Type { get; set; } public TextBox TextBox { get; set; } public ComboBox ComboBox { get; set; } } private List<LocationControl> _myControls = new List<LocationControl>();
Then, create type-control links:
private void InitControls() { // a better approach might be to create all controls here, // but if you want to use the VS designer, this will do // (just call it inside OnLoad or something) _myControls.Add(new LocationControl() { Type = LocationType.Province, TextBox = txtProvince, ComboBox = ddlProvince }); _myControls.Add(new LocationControl() { Type = LocationType.District, TextBox = txtDistrict, ComboBox = ddlDistrict }); ... }
Once you have this information, enabling the right control is trivial:
private void EnableRelevantLocationControls(LocationType type) { // enable this input var activeLocation = _myControls.Find(c => c.Type == type); Enable(activeLocation); // disable all other inputs var inactiveLocations = _myControls.Where(c => c.Type != type); foreach (var location in inactiveLocations) Disable(location); }