How to Bind Data to an ASP.NET ListBox Control? - asp.net

How can I bind data from a database to a ListBox control in ASP.NET?

Basically, you:
make a connection to the database and select the data (there are a whole bunch of variables that will dictate the best way to do this)
ensure your data is in an IEnumerable container, such as List< object>
assign the IEnumerable list to the Items/ItemSource property of the control
call DataBind() on the control
Of course this won't work for every control, a textbox will have to be done differently. This probably didn't help you much at all, when you have more exact questions then people can give you more exact answers. If you ask vague questions with a lot of different ways of achieving the desired outcome then nobody can give you exact step by step instructions.

See Web Forms Data on the http://asp.net site.

Related

Using more than one table with linq datasource on custom control

I'm probably opening myself up to a world of trouble with this one but here goes. As part of a project I have to have a method of representing a timetable for university students, showing lectures, times, rooms etc. I'm not a coding pro so I have found (and ok'd with uni) an opensource control by the name of DayPilot lite www.daypilot.org. I have that installed now and am beginning the arduous task of figuring out what to do with it. I know I need to bind it to a datasource. My problem is that to give all the information I have mentioned, I need to pull data from 3 tables. When I go to set up a LinqDataSource on the aspx page I can only pick tables from one. I have been searching for ages trying to locate some info. This is the best I have came across: http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx.
However it does not make sense to me. I can't see where he actually connects more than one table to the datasource on the aspx page, in asp code. Am I missing something glaringly obvious here. Is it perhaps a case that I connect it to one table through the linq DS and then write queries to pull the rest of the data I need. In that case how would the data be shown in the calendar if I only connect to one table. Here is the asp code for the calendar anyway.
`<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="OrionDataClassesDataContext" EntityTypeName=""
Select="new (Module, Building_code, Event_type)" TableName="Events">
</asp:LinqDataSource>
<DayPilot:DayPilotCalendar ID="DayPilotCalendar1" runat="server" />`
Obviously that is very preliminary. Plenty of work to do. If someone could give some (simple) advice it would be great. Thanks....and before anyone says it, I know I need to get rid of the default names haha.
Well, I have never used LinqDataSource, or anything on the direction of what you are doing right now. I'm still using the all sql-statement-in-code-behind method. So my idea below might not work.
If I understand correctly your question above, define a view to return result of your join tables, and add the view to your DataContext. In the LinqDataSource control, set the Tablename property to the view.

Separate ASP.NET MVC View object for each Model CRUD operation?

Most MVC tutorials I've been reading seem to create 4 View objects for each Model. For example, if my Model is "Foo", there seem to be 4 .cshtml files: Foo/Create, Foo/Delete, Foo/Details, and Foo/Edit. Using the VisualStudio "scaffolding" helper does this as well.
Is this really considered MVC best-practice? It just feels wrong to have 4 classes that are 80-90% identical to each other. When I add a new field to Foo, I need to edit all 4 .cshtml files. This sort of dual-maintenance (quad-maintenance?) just makes my OO skin crawl.
Please tell me: is there an expected/accepted best-practice which handles this differently? Or, if this really IS accepted best-practice, tell me why the quad-maintance shouldn't make me squirm.
I'm a reasonably skilled veteran of ASP.NET / c# / OO Design, but pretty new to MVC; so apologies if this is a noob question. Thanks in advance for your help!
Edit: thanks for all the replies! I marked the most thorough one as the answer, but upvoted all that were helpful.
You'll probably need between two and four different views:
List (for viewing many things)
View (for viewing a single thing. Might not be necessary, if it's OK to use Edit as View, or if List has room to show all properties)
Create
Edit (can be the same as create, if you code cleverly)
Thus, if your model doesn't have too many properties to show them all in a table, and if you're OK with not having a static, non-editable view for just examining, you can get well away with just List and Edit, and scrap the other two.
However, this doesn't solve your problem of double (or triple) maintenance if you update your model. There's other magic for that ;)
In ASP.NET MVC 3, there are extensions on HtmlHelper that let you do Html.DisplayForModel() and Html.EditorForModel(). These use predefined templates to nest themselves into your object and draw up display/edit fields for all public properites. If you pass DisplayForModel an IEnumerable<Foo>, it will create a table with column headers that are the property names of Foo (using the DisplayName attribute information if you supplied it) and where each row represent one Foo instance. If you give EditorForModel a Foo, it will create a <label> and an <input> for each public property on Foo.
All of the templates used by these powerful extension methods can be replaced by you, if you're not happy with the defaults. This can be done either on the level of Foo, in which case you'd be back in your double-maintenance scenario, or on lower levels (such as string or DateTime) to affect all editor/display fields generated with the templates.
For more information on exactly how this works, google "ASP.NET MVC 3 editor templates" and read a couple of tutorials. They'll explain the details much better than I could.
The views that ASP.NET MVC create for you don't necessarily need to be the views that you use in production. I found those just to be handy while developing quick prototypes or to test the database CRUD operations. Feel free to create whatever view(s) you would like to handle the operations.
I would generally just have 1 or 2 views to handle the basic operations and not use the built in views that are generated. For example, 1 view for adding, editing, or details and 1 view to show a list of objects.
It all depends on your application.
If you have a single item, you don't need a List view. If you can't edit it, then you don't need an edit view. Create and Edit can often be the same view, unless there are special things you need to do in one, but not the other.
In other words, use as many views as you need. There's no hard and fast rule here. The scaffolding is just there to help you on your way. Many kinds of apps will work just fine using the scaffolding, and won't require advanced HTML or Javascript.
Why would you want multiple views? Well, let's take the display and edit functions. You could create one view, in which you use if statements to determine the edit mode of the view, however this will complicated the view logic and views should be as simple as possible.
The reason to create seperate views is that its easier to maintain than one gigantic view with tons of conditional logic in it.
You can use exactly the same view when you are performing [HttpGet]. Given that you pass a proper ViewModel to this view, it will populate with appropriate data every time whether you are loading create, update, or delete Action.
The problem becomes apparent when you try to post that data to a specific Action.
Naturally View should have only one form, which will be used for posting data. When you declare this form, you specify which exactly Action to use for Post.
Having 3 different Submit buttons in that form will not make a difference since all of them will post the same form to the same Action.
You could do some javascript tweaking on OnClick event for these buttons to change Action to which data is posted, but this definitively would not be best practice.
Buttom line: having 4 different views for each of the CRUD actions is the best practice for MVC.
I tend to create the following for an object's CRUD ops:
index
_form (partial)
new
update
delete
view
As the same form is shared between new and update, there is very little difference between the two. It really depends on how much you want the variation to be, honestly.
As for delete, this is optional. I like to have a view in case javascript is disabled.
edit:
You mention view models and the guy above posted a long, convoluted (no offense) VM code sample.
Personally, I hate classes written to basically mirror domain objects and are only used to "move" data. I hate VMs. I hate DTOs. I hate everything that makes me have to write more code than is necessary.
I guess I've drank the coolaid of other frameworks (rails, sinatra, node.js) to the point where I can't stomach the idea of tossing DRY to the wind.
I personally say skip um.
Edit2 I forgot list..

Easy way to replace Data with ComboBox? Or populate ComboBox from Data

I took over a very old project written in VB6, it's a mess and I don't really know VB so it would be great if someone could help me.
I have a Data control which gets records from an Access database which I would like to replace with some kind of drop down list - I thought about ComboBox. Unfortunately I cannot get records from the database in the same way as with Data, so I thought about populating ComboBox based on what's in the Data control. How can I achieve it? Or is there a better way to do it?
My last resort is to create all database methods manually in order to replicate Data's behaviour for ComboBox but I think it's too much effort for such a small change.
Thanks in advance!
The Data control does more than just let them browse through the records, it manages the database, current record, data binding, etc.
The easiest way to do what you want is to hide/move the data control and use its .Recordset.Find*() methods.
Note that DAO is archaic and has been supersceded at least once for VB6 (with ADO)

Calling WebService for widgets or composite controls

I have a widget that contains about 40 controls in it. Essentially when a user makes a selection from a Dropdownlist on my main user control, what I want to do is call a webservice to get the new values for the controls in my widget. Is it efficient to have each control essentially call the webservice to get values that pertain to it’s new state Or is there a way to have the widget as a whole get the new values in a collection and then set each individual control within itself with the new values that apply, without really getting into a situation where you have too much going on and therefore loosing performance values
I understand your pain there. The latter option I think would be more efficient, but you have to provide a way to let each control pull out the relevant information that it would need. So you download the information once, then each of the 40 controls goes through a process of extracting the information only it needs.
If this is done on the client-side, it doesn't matter as much since you are passing the work off to the client, but still the latter option would be more performant IMHO. The question is how to get it to work efficiently and be easy to maintain. That's always the challenge.
HTH

How to get business objects and UI controls to talk to each other

I've got a person object with a name and age property that implements INotifyPropertyChanged. I want to hook this object up to an ASP.NET form so that the 'name' and 'age' properties bind to textboxes in a way that, when changes happen in either place (in the control or in the object) the other will get updated.
Do I create an intermediary class that listens to each textbox change events and the objects change events and handle the updates between them? What's the best way to do this?
I'm unclear on how to get business objects and the UI talking to each other.
I've stressed over this exact problem a lot.
The short answer is, yes, an intermediate item.
The trick is to NOT write ANY code per control. You should be able to place a GUI control on the screen (That may or may not take code), and then bind your business logic to it through a generic binding mechanism.
I have defined the bindings through XML, through properties files, and through constant arrays--there are a million ways...
You probably have to write code per TYPE of object bound (a listbox binds differently than a text control) and you may have to write validators (but specifying the parameters to the validators and which control the validators bind to should also be done in data)
Now all that said, I'd be really surprised if some data-driven auto-binding mechanism didn't already exist, Microsoft has been into that since VB first came out (although their implementations used to be pretty inflexible, I'm sure they do a better job now).
I'm very insistent about the 0 lines of code per control because my job has typically involved configuring complex devices with dozens of pages of controls. A typical client/server system will have 7(!) lines of code PER CONTROL just to transport data from the DB, to the server, to the client, to the screen and back (this is a minimum for plain ole "dumb" code with no smart binding tricks).
0LOC/control may not be a requirement for everyone, but it's a good goal.
Comment response:
I've done most of my stuff manually in Java, so I'm not sure I can be too much help with the specifics.
Searching for C# and binding gave me this which looks promising, although it may be binding straight to a database which is too much IMO, it should bind to a business object, but the concepts should be the same.
One way to create the bindings at first is to manually instantiate binding objects... (Please excuse my Java)
TextControl textCtrl1=new TextControl("Name Goes Here");
new TextBinder(textCtrl1, personObject, nameField);
In Java, that second line gets tricky. When you are binding to a particular field, you HAVE to use reflection to find the setter and getter for that field of the personObject. In C# I think it should be easier.
Anyway, the binder should add itself as a listener to the control and the object, then forward changes back and forth.
Does that help any?
Edit2:
As you noticed, the hard part is noticing when your property is updated. Luckily, that is optional. More often than not, you don't need to update the component once the object is set (I had to deal with this a few times when I had distributed UIs that could update each other).
So, if you assume your object won't change, the "Binding" has to do the following:
get the value from the property and set it in the component.
add itself as a listener to the component.
store the property/object (if you can manipulate properties, you're set here. If not, you need to store the object and property name, and use reflection)
bail and wait for an "updated" event from your component.
When you get the update from your component:
- store the value in the property.
- You may want to set an "Updated" flag or store the original so that if you iterate through all the binding components, you can tell if any updates need to be saved/enable the "ok" button.
Your object should always be pretty much up-to-date now.
As you build a form, you may want to put all your binding controls into a collection so that you can do a few other operations...
A "Save" operation could call each binding control and tell it to copy from the control to the property, that way you don't need to use a listener.
A "Reset" operation can reset all the controls to their original value.
A "Test" operation can ask each control if it's been updated.
. etc
The neat thing about doing it this way is that every "Operation" you wish to add is pretty trivial to add, but automatically affects the entire UI.
You probably also want a little object hierarchy of controls with an abstract base "bind" class, then a specific binder for each type of control (text field, number field, date, spinner, table, pulldown)--I think that's about it.
This can be very simple, but gains complexity rapidly. Try it with a text field and see what you can do. A simple text binding object should just be like 5 lines of code if you can pass "properties" around in C#...
Okay, totally separate answer. As I told you, I'm not very up-to-date with C# technologies, but from what I've heard, LINQ may do this entire job for you.
In fact, LINQ may be made to do exactly what you are trying to do. It doesn't exist in Java, so that's why I gave you the "Manual" version in the other answer.
The comment at the bottom of this page: http://msdn.microsoft.com/en-us/library/z919e8tw.aspx alludes to a better way.

Resources