IMHO: DataBinding for ASP.NET list controls needs improvement (VS.NET 2003)

J

JV

It's easy to databind a listbox or dropdownlist if all you want is to fill
it with a list of values. There are plenty of examples in the online help.

Unfortunately, real world applications demand more. Usually you would also
like to bind the SelectedValue to something else. Common scenario:

You have an ADDRESS table with a foreign key to a STATE table. You join on
an integer or uniqueidentifier key value, but the STATE table is what
contains the human-readable state name and abbreviation. Now you'd like a
web form for adding a new address to the table. So you want to really bind
to a single row in the address table. However, you would like a
DropDownList (never understood why they renamed "ComboBox" for the web??)
that displays a list of the states and lets the user select the state they
want. You just bind the list to the values in your STATE table, using
perhaps the state code "WA", "FL", etc. for the DataTextField and the STATE
table's primary key column for the DataValueField. When the user hits the
submit button, you just insert the dropdownlist's SelectedValue as the
foreign key. All fine and dandy.

Now what if you want to edit an existing row? Well, obviously you not only
want a dropdownlist populated with your states, you also need to pre-select
the value that is already in that row. You can do this, but there are
problems. Take a look at the (DataBindings) property of the dropdownlist.
Well, there's a really handy feature where you can bind SelectedValue to
something else on your form, perhaps a DataView which filters out only the
row you are editing. Now you are actually binding one control to two
different data sources. Seems really cool, huh? Well, this turns out not
to work well at all (give it a try). Details below if you are interested.

In actuality, you have to programmatically set the
DropDownList.SelectedValue yourself in the code. Yeah, it's great that you
can get it to work, but that is pretty bogus and kludgey in my opinion.
What is really needed is more sophisticated way to set databinding so that
these very commonly used foreign key relationships can easily be handled in
a web form.

--JV
------------------------------------------------------------------------------------------------------------------------------

Details of problem: You have to DataBind() the dropdownlist to the row you
want to edit. The first time I do this, it actually seems to work fine. The
data posts back properly and the update to the database & datagrid work. On
every post thereafter, the dropdownlist will always report that item 0 is
selected. Looks to me like it databound the list part properly, but not the
SelectedItem.
 
B

Brock Allen

Details of problem: You have to DataBind() the dropdownlist to the
row you want to edit. The first time I do this, it actually seems to
work fine. The data posts back properly and the update to the database
& datagrid work. On every post thereafter, the dropdownlist will
always report that item 0 is selected. Looks to me like it databound
the list part properly, but not the SelectedItem.

Are you data binding the DropDownList on every postback? If so then that
would explain the behavior you're seeing. In general the model is to only
build/data bind the controls when it's not a postback, as such:

void Page_Load()
{
if (!IsPostBack)
{
_statesDDL.DataSource = GetSomeDataViewTableOrSet();
_statesDDL.DataTextField = "StateDisplayName";
_statesDDL.DataValueField = "StatePK";
_statesDDL.DataBind();
}
}
 
J

JV

Yeah, i don't think you got the point of my post. Your code does not
DataBind the SelectedValue to the row in question.
 
J

JV

That is an interesting post. On a quick scan I don't think it's quite my
problem, though I will go over it in more detail. Here's why I don't think
it's the problem. My page works essentially like this:

1) user selects row to edit, causing a postback.
2) DataView filters out only the row to be edited, then the "edit dialog"
(just a hidden div on the same page) is displayed and all controls on it are
databound. In the case of the DropDownList this rebinds the list to a
DataTable and (in theory) the SelectedItem property is bound from the
DataView. All other controls bind only to the DataView row.
3) User sees current row settings, can alter them and click a SAVE CHANGES
(submit) button. Of course, this causes a postback. During page load, the
underlying DataSet and DataView are reconstructed from Session, but no
controls are DataBound
4) The button click code pulls the values from the controls on the edit
dialog, updates the database, then re-hides the dialog.

So, here's what's interesting: the first time, this works like a charm.
User selects row, dialog displays the selected row's settings accurately,
clicks Save, database is updated correctly.
Now, user clicks another row (same row, diff row, doesn't matter). Problem
1: Dialog box pops up with all the right settings EXCEPT that the
DropDownList has the first item selected regardless of the contents of the
DataView's row. Problem 2: User selects something in the list and hits
SAVE, but when the code for the button click tries to retrieve the selected
row from the list, it always reports that item 0 is selected. I've checked
both the SelectedValue and SelectedIndex. Now, there's nothing in the Page
Load code that should have caused this erroneous value to be posted. No
DataBind takes place in between the user clicking the button and the button
click code trying to retrieve values.

However, if I only bind the list on initial page load, then manually set the
SelectedItem to the edit-row's value instead of trying to databind it,
everything works properly. User posts back and data is correct. Perhaps
the secret as to why I have to do that is in your weblog.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top