More efficient way of doing this? (ASP.NET textboxes) - c#

I'm using a Wizard in my ASP.NET page, where in the first step the user chooses from a DropDownList, how many sets of controls will appear in the next wizard step (from 1-5).
For example, in the 2nd step of the wizard there are 3 textboxes. If they choose 2 on the previous screen, there will be 6 as there will be 2 sets of these.
I need to be able to store the contents of all these textboxes in a database (simple part I think, there's 5 columns and all can be null.
The easy way of doing this I think is just creating all of the possible controls (5 sets), and hiding them based on what they choose in the previous screen. Is there a more efficient/easier way?
Thanks

It really depends on your definition of efficient/easier.
A more standard approach would be to use a repeater control to display the correct number of controls based on previous input. However if you have not used a repeater control before there will be a degree of learning involved in displaying your output and retrieving user input during the postback.

You can use the ASP.NET Wizard Control

If you absolutely know that 5 boxes is the max, and it is highly unlikely that there would ever be more than that, using Control.Visible on the server controls and their interface items such as label or what ever else, would work... but...
It's a bit brittle of a solution, though; Requiring you to make manual code changes in a few places if you decide to add more possible boxes.
A dynamic solution would let you set a maximum number of options in config, or just a single place in code. It would probably require you to change your database structure a little bit, but that would likely be better for normalization, anyway. It involves dynamically generating the items in the step of the wizard, too.
(More info on that option can be had if desired!)

Related

Is WPF adequate for creating such a form or am I missing something?

Intro:
I am quite new to C#.
It is my job to create a certain simulation program.
I can't ask other programers, I'm an intern with a nonprogramer adviser and should not nother them. (I'm doing more of a fun project.)
So before spending a week or two to get really into WPF, I wanted to make sure it fits my needs.
If not, I want to use something else.
What I need:
Show the user a range of radio buttons to decide wether a simulation/plot/opening_of_svg should be done or not. (Can be done.)
A button "Add Task" where the user can choose one. (Can be done.)
Depending on the choice, add a certain form into the existing one, where parameters can be set. (Not found.)
Some of them radio buttons. (Can be done.)
Some of them text box, for integers and doubles only. (Can be done.)
All of them with default values. (Can be done.)
After adding a task, another "Add Task" button should be created, for adding more of them. (Not found.)
If for example a second simulation is added, it should take the values of the first one as default. (Can be done.)
Save the choices and data to a text file in a certain syntax like "Gnuplot Add Restriction = Time" which is used by the rest of the program. (Can probably be done.)
Question:
The biggest uncertainty is the one with adding forms into the forms.
Can this be done in WPF?
If there is any other framework (if this is the right name) specially made for this, feel free to comment.
I can't add many links due to my reputation. I might do it later. Just in case someone will find this later and wants to know about how.
Rather than creating Forms, consider creating UserControls and arranging them within a single Window. This is the same pattern you might use when creating composite windows in WinForms. I'd recommend the Grid or DockPanel layout panels.
In this respect, WPF is quite similar.
In addition, it sounds to me like you are doing a feasibility study for a new requirement. Are you sure it is not appropriate to speak to more senior staff? If I was them, I'd want to know your findings!
On reflection...
It seems like you are trying to create a branching Wizard workflow, like an installer might give you, i.e. Next, Next, I Agree, Next, Finish. WPF Can absolutely do this and some open source solutions already exist. For example, Avalon Wizard.

Creating a MessageBox with multiple Checkboxes

In a recent project I'm planning to allow the user to make certain configuration using a MessageBox with various CheckBoxes. The number of these CheckBoxes is variable and depending on the amount of entries the User made beforehand, so I don't know how big of a Box I need and how many CheckBoxes there will be inside it.
When the user is done checking and unchecking, he will press the "OK" Button and the values should be returned and saved.
Multiple questions to this whole thing:
1. Is this a good/logical way to approach this whole thing? (Having the user make yes/no configurations to an unknown amount of options)
2. How would I create a MessageBox/Pop-Up with an uncertain amount of CheckBoxes?
3. Is there any smart way to design that box, so that it is not to big or to small and fits every option evenly spaced?
Is this a good/logical way to approach this whole thing? (Having the user make yes/no configurations to an unknown amount of options)
Yes, what else could u do if the options to agree with depend on previous settings. So yes this design is ok.
How would I create a MessageBox/Pop-Up with an uncertain amount of CheckBoxes?
In general, i highly suggest to not use the standard MessageBox of the .NET Framework. I would make my own window, and place all in with an yes/no button.
Is there any smart way to design that box, so that it is not to big or to small and fits every option evenly spaced?
Design ur window based on a Grid. Then pick a place that can be ur viewing area. In this place add some kind of stack control ae StackPanel. Then just load dynamically ur controls into this stack and ur done.

Working with values in web forms scaffolding

I have recently been tasked with learning EF 6 and web forms scaffolding for our group. Most everything works perfect for me and even though I was ready to hate it I don't. There are a couple of things that I can not make work the way that I want them to. When I scaffold some CRUD pages it works great, but I want to set some default values on the Insert page. I am able to set values in the constructor or the properties, but they do not go to the insert page until the click event to add the item. I have tried every way I could think to figure out an annotation that would work on my object as well as in the Dynamic control. So far I can not make anything work. I can actually remove the dynamic control and add my own control and add it to the object in the insert method, but that just does not feel right. Is there another way of doing this?
Specifically the things that I am trying to do are to set a default date in some fields based on DateTime values over a 30 day period from creation time. I also have state and county dropdowns that are tied to more than just the object that I am working on. Currently I use EF6 and LINQ in my DAL to just populate the dropdowns with objectdatasources and they work great. I want to hook them as dynamic controls as well. I know I need the [DataType] attribute, but there is no dropdown option to choose from. Also I am not sure how to hook up the dropdowns to have the county fire after the state fires so that the counties will change with each state selection. Is there a tutorial somewhere that would cover these things. I have searched everywhere and can not find any guidance.
Thanks Jimmy
Since no one seems to know the answer I thought I would list the needed things to help you if you are having the same issues. If you need to work with the data inside a Dynamic control you are going to have to do a lot of conversions. You must find the Dynamic control in the container you are using and parse that to a dynamic control and then you need to dig into the control template of that control and find the textbox etc that you need and parse that into the needed control type and grab or set its value. If you are needing to keep your code secure and must pass Fortify or some other static analysis tool you will also have to encode the values because fortify can not see that it came from a control that was encoded. This becomes a pain in the butt and it is much easier just to go ahead and switch to a standard control and manually add that in the insert.
The line of code if you need to know how this would work is something like this.
((TextBox)(DynamicControl)fvName.FindControl("DynamicID").TemplateControl.FindControl("TextBox1")).Text;
I wrote this from hand so it might have something out of place, but this should get you on track.
Jimmy

Should I create multiple ASP.Net ASCX Controls or a single Control to do multiple things

I have a website that regularly has 500 simultaneous viewers. I am looking for some guidelines for using ASCX controls.
I want to make sure that the website has a fast response time, but I also want to re-use my code.
I am creating a custom control that displays a table full of information. The table is paginated (containing page numbers and prev/next buttons above the table). I want this control to be reusable and in some cases the page numbers may be displayed below the table as well or instead of above it.
I am aware of the GridView Control, but I want a custom control in this case.
Should I create a separate control for the page numbers? Or should I have one large control with top buttons, middle content, and bottom buttons all in it? The page numbers are derived from the same data that the data table is derived from. If I create separate controls I will need to pull this data each time the control is called from the page correct?
How would you do this?
In that case you probably want a single control.
You might make multiple custom controls (one for the page numbers, one for the table, etc.) and then another one that aggregates the simpler ones into your master view of the data.
One thing thouh, I would definitely make sure to decouple the data from the presentation. Don't tie your control to the data!
The rule above implies for instance that when making your control to represent the page numbers, or the buttons, those should be abstract enough to be controlled by your master control, or to represent actions on the _master_control not necessarily interact with the data of the master control directly.
Do you anticipate creating grids without pagination? If you do, it might be good to make them separate controls. However, if you decide to make it one big control, code it in a way that makes separating them easy.
If performance is an issue, make sure that your controls output the least amount of HTML possible, and cache your outputs.

Guidelines for using the ASP.NET Wizard efficiently

A web app our group has put together uses the ASP.NET Wizard control. Each step in the wizard contains a corresponding custom UserControl.
The trouble is that as you go through the wizard steps, all the UserControls are loaded on each step, rather than just the one used in that step. That doesn't seem right to me.
So...is there anybody here that's done a lot of work with the Wizard control and can give some guidelines on how to use it correctly, and keep it from loading way too much junk with each step?
One thing that could help you a bit is not putting any code in your UserControls's Page_Load function but instead putting that same code in it's Page_PreRender. That's crucial when using a MultiView and probably applies to the wizard as well.
mspmsp has a good recommendation about PreRender, another option that I have noticed used before is to simply move all configuration code inside the control to a ConfigureControl method.
Then when switching views, you can call the ConfigureControl() method to explicitly create/load your control. It has the same purpose, but it helps make the code a bit easier to understand in my opinion.
FYI, (at least part of) the reason it loads all user controls on each step is so that you can access the values entered on other steps. If it didn't load the controls, you couldn't easily make decisions about the current step based on what was entered in a previous step (e.g. filtering a list based on a selection in a previous step).

Categories

Resources