user194076
user194076

Reputation: 9017

Conditions in usercontrol

let's say I have a user control with dropdowns/textboxes/gridviews etc. All of this is controlled by a parameter(or several parameters)

let's say I'm adding my usercontrol to a page and set something like:

userControl1.Type = Advanced;

Or

userControl1.Type = Regular;

Then in my usercontrol in multiple places I have something like:

    if Type ==Advanced
    gridview.DataSource=dataTableAdvanced;
    else if Type==Regular
    gridview.DataSource = dataTableRegular;

Or something like:

if Type==Advanced
dropdown1.Visible=true
else
dropdown1.Visible=false

Control is getting cluttered quickly if I have parameter with, let's say five different values available. Is there a better technique to do this?

Upvotes: 1

Views: 121

Answers (2)

Eric H
Eric H

Reputation: 1789

Your Advanced control can derive from your Regular control and override a method that sets the dependent objects.

class RegularControl
{
   public virtual void SetStuff() { //visible, data source, etc }
}
class AdvancedControl: RegularControl
{
   public override void SetStuff() { // same deal }
}

Upvotes: 4

Mike Burton
Mike Burton

Reputation: 3020

It sounds like you should really have 2 controls, one Advanced and one Regular.

Upvotes: 1

Related Questions