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.
Related
up till now i have always created and bind data source on code behind but now i have seen (and used) object date sources on aspx page and bind right there by mentioning listview DataSourceId. if i just want to display data without any change, does it make any difference? in performance or in term of good practice?
As discussed in one of SO question
asp.net sqldatasource vs doing it in code behind
Embedding your SQLDataSource or any datasource within the asp.net page is coupling your presentation layer with your data access layer resulting in reduced testability and flexibility. I strongly suggest moving your data connections to their own classes and created a data access layer that your code behind pages could then draw from.
Ideally you'd seperate this even further into an N-Tier solution. Link
Some Useful links
populate gridview via code-behind or markup DataSource?
I would say that it depends on size and scalability of project.
If you want power and control then go for code-behind.
If you want ease-of-use and speed then do things on the page and let the object manage the CRUD
I would say you have alot more control over your control if you bind it in the code behind you can manipulate your results in many creative ways. If you do your databinding in the your markup like with a SelectMethod or OnInit. Every postback or reload will put that data back to what you have in that method. Which can be great for populating a drop down menu that you always want to show the same data. If you want your data to be responsive i would say you have to use DataBind() in your code behind.
I would also think this is a best practice for learning to do more advanced things with your data.
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 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.
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.
I am looking for an example of using ASP.net AJAX to show a 'live' filtering of a repeater control based on what is being typed into a textbox. I have seen stuff using the Web Client Software Factory but am more interested in something that doesn't require an additional library.
The asp.net ajax control toolkit has one here.
If you don't like that one, searching google for "Ajax Autocomplete" gives lots of decent looking results, unless I am mistaking what it is you want to do.
I think you're a little confused as to what AJAX entails. If you want the filtering to interface with the server control's datasource, then it would not by definition be AJAX.
Live filtering a databound control the way you want is ill-advised, I think. For each iteration you'd have to re-bind the control, which would create a tremendous amount of overhead.
You'll need to find a way to do this client-side, with javascript. It could simply hide items within the repeater-created markup as they are filtered out, although how exactly this would be accomplished depends entirely on what html you're generating with the repeater.