473,508 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 19 '05 #1
4 1313
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();
}
}

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #2
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.

"Brock Allen" <ba****@develop.com.i_hate_spam_too> wrote in message
news:22*********************@msnews.microsoft.com. ..
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();
}
}

Nov 19 '05 #3
Perhaps this is your issue then?

http://weblogs.asp.net/despos/archiv...16/394834.aspx

-Brock
DevelopMentor
http://staff.develop.com/ballen
Yeah, i don't think you got the point of my post. Your code does not
DataBind the SelectedValue to the row in question.

"Brock Allen" <ba****@develop.com.i_hate_spam_too> wrote in message
news:22*********************@msnews.microsoft.com. ..
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();
}
}


Nov 19 '05 #4
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.
"Brock Allen" <ba****@develop.com.i_hate_spam_too> wrote in message
news:22*********************@msnews.microsoft.com. ..
Perhaps this is your issue then?

http://weblogs.asp.net/despos/archiv...16/394834.aspx

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2201
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
7
2185
by: MLH | last post by:
Sub ListControlsBttn_Click () '************************************************************************* ' Purpose: Run the Controls Collection for user-specified form. ' The controls collection...
1
1091
by: | last post by:
Hi!! I'm looking for some ASP.NET controls in Office 2003 web access style. Mainly an Outlook 2003 web access calendar control. Is something like this anywhere available? Regards, Gicio
1
1055
by: Rob Nicholson | last post by:
Any recommendations for a 3rd party server control to replace the basic HTML SELECT mechanism? We have encountered a problem with the control in the NetAdvantage suite with their WebCombo and can't...
2
1627
by: Joanne | last post by:
Hi, I have an "interesting" problem with UserControls in a datagrid and I'm desperate for your help as I work alone and have no-one else to ask. It is quite complicated but I'll try to keep it...
0
928
by: Agnes | last post by:
in my form, there is tab page 1 and tab page2, I use databinding to bind all the textbox. 1) user press "new" button, bmMyInvoice.addnew() 2) user do some calcluation in page1, e.g txtInvTotalAmt...
13
2393
by: Michael.Suarez | last post by:
Let's suppose I have an instance of MyClass, which has the properties MyText1 and MyText2, and sets the values of MyText1 and MyText2 to 'A' and 'B' in it's constructor. Let's also suppose I...
0
1363
by: aaragon | last post by:
Hi everyond. I'm trying to write a library usign policy based design so I can implement different behaviors. One of the behaviors was to define a StoragePolicy. The following code gives the...
4
3752
by: AC [MVP MCMS] | last post by:
Implementing databinding from the ASPX/ASCX file is very straightforward: <asp:XmlDataSource id="XmlDs1" DataFile="">...</> <asp:Repeater> <HeaderTemplate><%#XPath("title")%></> </> However,...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.