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.
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.