Working with values in web forms scaffolding - c#

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

Related

WPF AutoCompleteBox Data Virtualization

I am trying to implement Data Virtualization on a WPF AutoCompleteBox.
I found Bea Stollnitz's code here which works great on a ListView and I made it to work on an ComboBox easily, but there's no way I can seem to get it working right on an AutoCompleteBox.
To be precise, it works - the list is virtualized, items are not all loaded on startup - but what seems to happen is that the AutoCompleteBox iterates through all the items in the list as soon as the ItemsSource changes and this ends up breaking the whole thing (i.e. on load, all the pages in the list are requested, so even though they are virtualized, they will all be requested in the beginning and get loaded). My guess is that this is because of the filtering that the ACBox does, but I'm surprised that there's not way to prevent it, since the control does normally allow filtering to be done on the server-side using the Populating event.
I feel that I'm missing something, I can't believe that no one has done something like this before or that it can't be done, so I'm guessing that I'm just doing something obviously wrong that I can't figure out since I'm new to WPF.
Here's some of the things that I've done to try and get this together (based on Internet searches around similar problems):
I made sure all the conditions for the built-in UI virtualization are ok including explicitly turning it on, setting the max height of the ListBox etc.
I replaced the ListBox in the AutoCompleteBox with a ListView like the one that Bea uses in her example. Side by side, the ListView by itself works as expected, but the one embedded in the ACBox does not.
I tried using no filtering in the ACBox, using a custom filter, handling the populating evenet manually etc. This doesn't help. Making a custom filter is obviously not enough since the custom filter only allows you to specify the result of evaluating one item, the code that loops through the list is not visible so you can't prevent looping. Turning the filter to "None" doesn't do anything either.
Any and all suggestions are welcome!
The target for this is .NET 3.5 and I am using the WPF Toolkit (Feb 2010 release)
Thank you!
I tracked this down to OnItemsSourceChanged() (see source). In there, the AutocompleteBox stores a "local cached copy of the data", which is why I see the behavior noted above.
This is a private method, so no overriding here.
It seems to me that because of this you can't apply DataVirtualization to the AutoCompleteBox, at least not using the ideas in Bea's solution. If anyone has any different thoughts regarding this, I would love to try it out, but until then, this is what I believe the answer to be.

Should I Use User Controls If I'm Not Going To Reuse The Code?

First of all, I hope I'd get some advice about my practice because based on the very few books I've read, they didn't write much in the aspx page..they just built some controls and used them in the aspx page, so is this approach a good practice ?
here comes my question, I thought using web controls instead of directly writing into .aspx page is better as I could reuse the code, but now I'm creating those controls and I don't think I'll reuse them again or maybe only just one more time.
so do you think it's wise to create a control for the code instead of directly coding in the .aspx page ?
I was also working on a web user control for adding a new item to my db, and then I started planing for the update or edit control..I thought maybe I'd use the same control for both add and edit and start reusing my code, and on my way editing the control to be able to function as both add and edit control, I started with adding properties to the control, then a couple assignments in the Load method, then some checks with if...So I realized maybe a new control would be better!
I don't know, I'm thinking intuitively but I could really use a professional, experienced point of view.
Thanks for your time =)
Sometimes creating a user control, allows you to encapsulate some specific logic and ui elements into a separate class. Even if you are not reusing the control, the final code may be simpler to read and maintain. Take by example a Login control, if you take login related decisions in the user control and make those 'details' hidden in the rest of your code, then the code get simpler and easier to read and mantain!
If you're not going to reuse the code, then you don't want a user control or any other kind of control. Just put the appropriate code and controls onto the page.
If you find later that you do want to reuse it, then you can make a user control out of it.
If you know for sure that you are going to want to use a control (or some slight variation) then creating the user control is a no brainer.
For me, if it occurs to me that I may need similar functionality again in some future project then I will sometimes create a control just because I think it will be useful.

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

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!)

Serializing MDI Winforms for persistency

basically my project is an MDI Winform application where a user can customize the interface by adding various controls and changing the layout. I would like to be able to save the state of the application for each user.
I have done quite a bit of searching and found these:
How to auto save and auto load all properties in winforms C#?
Save WinForm or Controls to File
Basically from what I understand, the best approach is to serialize the data to XML, however winform controls are not serializable, so I would have use surrogate classes:
http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx
Now, do I need to write a surrogate class for each of my controls? I would need to write some sort of a recursive algorithm to save all my controls, what is the best approach to do accomplish that? How would I then restore all the windows, should I use the memento design pattern for that? If I want to implement multiple users later, should I use Nhibernate to store all the object data in a database? I am still trying to wrap my head around the problem and if anyone has any experience or advice I would greatly appreciate it, thanks
You don't want to serialize the actual control instances. They should be created and destroyed along with the Form they reside in. Rather look at what you let the user customize. Layout and position? Very well, save out the Top and Left coordinates for each control along with a control identifier. Do you let the user add new controls? Save their ids along with a type identifier so when its time to reload you are able to recreate the controls at their previous position.
Whether you use XML or some other format, there is no best approach or best practice, choose what makes sense for your project. XML happens to be an easy to go with format with great support in the .Net Framework.
I know there is a software, LinsUI Layout Manager, which handle your problems very well. They have free version for interested developers. You can check the site.
Cheer

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