I created a simple detail edit form earlier, and decided to data bind some controls on it. Of course, I was told they needed to be on a data bound container. My immediate, rather uninformed choice was a FormView. Is this appropriate? What containers could I use here?
Wrapping my controls in the ItemTemplate of the FormView of course made them inaccessible to my code, forcing me into many FindControls and casts, which is just untidy. I know I can write helpers and extension methods that make this much neater, but I just wanted a quick demo. Am I missing something regarding dealing with templated, and this 'nested' controls in this situation?
Your choices are the FormView or DetailsView controls. The primary difference between them is that DetailsView does all the work for you whereas FormView requires you to create your own templates, thus allowing greater control. As you've discovered, FormView requires using FindControl to access the controls, but the intent with data binding is that you shouldn't need to frequently access the controls directly.
Related
I am an asp.net beginner and thinking about designing a website with a typically Master-Detail view. There is a GridView-Control which is displaying all Records and Detail view below to edit existing records, add new ones and display one in detail.
There are several controls in asp.net which can handle such situations: GridView in combination with DetailsView or FormView. But all these approaches do have in common that they seems to be designed for Rapid application development. I want to use my own DAL and so on, so I need to have full control over Insert/Update statements for example.
Whats the typical asp.net approach for dealing with this?
Should I create a UserControl for the Detail view which saves its state (View/Edit/... mode) on its own? Furthermore the view differs only slightly with its state (for example the Insert view does have one more Input-control than the edit view). It seems that the mentioned DetailsView and FormView cannot handle this either and so I have a lot of copy&paste like code.
I think thats all a pretty common situation. What do you prefer in those situations?
You can bind manually data from your custom DAL to the DetailsView or FormView directly like so:
this.dvw.DataSource = new[] { DAL.GetObject(1) };
this.dvw.DataBind();
Or you can also use the ObjectDataSource, which wires up to your DAL object and invokes the method when it needs it.
You can also use a custom user control and load the data manually, which is an approach I have taken in my applications too.
It really depends on your architecture, what you want to achieve, how complex your object model is, and a variety of other factors.
For the Master part, I build the interface by hand, I mean, creating textboxes, labels, etc.
For the Details part, I use a editable GridView. This a tedious task.
Of course, you can use some scaffolding to generate ASP.Net code for your UI from the database model.
You can check this: http://codepaste.net/b1geac
I have an ASP.NET page with a Wizard control containing several pages of form fields. The data is collected and inserted to a database from the code behind page. I need to set this form up so you can not only insert, but edit a record as well. Since the form is long and complex, I would rather use the existing one and not make a duplicate one for editing, especially since I want to keep both forms exactly the same and any edits would have to be made to both. But it looks like this is what I need to do if I'm going to databind it. But this would also involve putting the Wizard inside of a FormView, and then I'd have to use FindControl to access any of the fields which would mean altering all my already-existing code (which of course would be time-consuming). So should I manually enter all the values from the code behind instead of databinding it? Which is better, to use a FormView and have duplicate forms (plus have to go in and redo the way I access the fields), or to do everything from the code behind?
I cheat in this circumstance. :)
Create each screen as 2 separate user controls
One for edit, and one for view
Then you get access to all you usual coding
Then embed the controls into the Wizard/FormView
I would suggest you to Go Ahead using FormView, as using DataBind control you have more control the functionality and layout Insert/Edit/View template. Since you have specified that your form is very complex and long, if you control from code behind you have to do lot of work to handle this in code behind and lot code required.
Since I have personal experience to develope very complex form using FormView and it was easy for me bind the Value in directly in formview instead if you assign/Get Values of each conrol in code behind and sometimes you have to hide.
I don't much care for the template rigmarole of the ASP.NET FormView web forms control. It seems the only way to access template defined controls is to 'capture' module level references to the wanted controls in the ItemCreated data binding event, for use in other tasks and event handlers.
Ideally I would just like to be able to call set this.DataSource and call this.DataBind on a page, but only the latter is possible through inheritence, and doesn't achieve any of my normal data binding needs.
What else is there besides the hairy, scary FormView control?
There is the DetailsView control which renders a 2 column table to setup a form. Very uniform control though, so there is limited flexibility if you need that.
Are you looking for all .NET framework or also any OSS or third party?
HTH.
There are two primary methods of getting data onto a page when it comes to associating that data to markup using some kind of Repeater; performing data binding in the markup (DataBinder.Eval, etc.) and overriding the OnItemDataBound event, finding the control on the page, and setting the value on the control in the code behind. I have my own opinion and understanding based on the MS-70-528 exam, but I wonder what the consensus here on SO is; what's the "best practice" or "accepted design pattern"? Even when there's some operations to perform on the data being bound.
Try binding in markup as you can, that code is more maintainable than the code in OnItemDataBound. The intention is what ever is the simpler.
I am looking for some easy to use data binding to forms controls. Something that will handle formatting, validation and error handling, something that will handle filling controls from business object/DTOs and vice versa with minimal code. I did use google and have found these two links:
Implementing two-way Data Binding for ASP.NET
Using Reflection to Bind Business Objects to ASP.NET Form Controls
I am curios if there is something newer and more complete.
Are you using FormView or manualy fill controls and variables or something else?
I have always used the standard asp.net DetailsView/FormsView controls along with either SqlDataSource or ObjectDataSource controls to accomplish what you are trying to do. This will allow you to do two-way data binding and with a small amount of coding with template fields you can add validation and formatting.
Take a look at http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/formview.aspx for more information.