• 0

C# Dynamic User Interface Controls


Question

Hi Guys

 

Need a little advice. I'm trying to create a webform that displays controls based on a selection by a user, For example if i had a dropdownlist that said transport with three options car, train or aeroplane then depending on what was selected the following should happen

 

if car was selected then textboxes with labels displaying colour, make, model, engine

if aeroplance was selected textboxes with labels displaying make, engines, seats, wing size etc..

if train etc..

 

Whats the best way to tackle to this?

 

Thanks!

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Usually what I do is have different divs set up with those controls in it for the selections (if you know the selections will always be there), then use javascript to show or hide the appropriate div and post that data back.

Link to comment
Share on other sites

  • 0

But if I then want to store the selected values into a SQL database could I without checking which controls have data in them. 

 

Could I even store the criteria in a sql database and then create the controls at runtime?

Link to comment
Share on other sites

  • 0

You would already know which fields to use because of which transport option was selected.  Instead of checking all of them, you'd only need to use an "if" statement to determine which option was selected and go from there.  You can add them dynamically at runtime, but it would be more work up front.  If it's a simple task, it may not be worth the extra effort needed to create them at runtime, but that decision is yours to make based on the amount of time you have to do it and how much effort you want to put into it.

Link to comment
Share on other sites

  • 0

I see what you're saying. The simpler solution is often the best.

 

What are the downsides to this method? If I have more transport options then I'll have to incorporate all the questions into that single page and enable at runtime the options requested.

Link to comment
Share on other sites

  • 0

The biggest downside is that you have all the controls for each option on the page at the same time, so your html markup will grow as a result.  That's why I said it'll really depend on how many options you plan on having and if you want to put the work in to create the controls dynamically or not.  If it's just a simple form though, I say just pick the simplest way unless you want more experience creating dynamic form controls.

Link to comment
Share on other sites

  • 0

The biggest downside is that you have all the controls for each option on the page at the same time, so your html markup will grow as a result.

I don't believe that's true if you set style="display:none" (then enable it dynamically with JS/jQuery) The HTML just won't get created.

Link to comment
Share on other sites

This topic is now closed to further replies.