473,799 Members | 3,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataBound ListBox

I have the following code (this is a snippet):

InitDBConn();
OpenDBConnectio n();

OleDbCommand cmdRidge = new OleDbCommand("S ELECT * FROM
TRDRidgeTypes", oleDbConn);

OleDbDataReader oleRidges= cmdRidge.Execut eReader();

if (!this.IsPostBa ck)
{
ddlRidge.DataSo urce = oleRidges;
ddlRidge.DataMe mber = "TRDRidgeTypes" ;
ddlRidge.DataTe xtField = "RidgeType" ;
ddlRidge.DataVa lueField = "RidgeTypeI D";
ddlRidge.DataBi nd();
}
----
This code successfully fills a DropDownList box. However, when the
user selects one of the items and the page is posted to the server
,the SelectedItem property of the List is null and the SelectedIndex i
(-1). I am tearing my hair out because I have found people on the
newsgroup with a similar
problem, but it was due to their not setting data source in the
"this.PostB ack =False" condition. I have done that, and it still does
not help me.

I do have ViewState for the control set to True. I tried hardcoding
the ListItems in the HTML and the control values were read correctly.

I am sure I am missing the obvious here..but please help!!

Thanks,
Stacey
Nov 17 '05 #1
2 4749
You don't want to bind the data on post-back.
You are wiping out the results when you bind the data!

only bind when postback is TRUE, or when you know the
content will/should be changing. Otherwise, the viewstate
will handle everything else for you automatically.

email if you need more...
-----Original Message-----
I have the following code (this is a snippet):

InitDBConn() ;
OpenDBConnecti on();

OleDbCommand cmdRidge = new OleDbCommand("S ELECT * FROM
TRDRidgeTypes" , oleDbConn);

OleDbDataReade r oleRidges= cmdRidge.Execut eReader();

if (!this.IsPostBa ck)
{
ddlRidge.DataS ource = oleRidges;
ddlRidge.DataM ember = "TRDRidgeTypes" ;
ddlRidge.DataT extField = "RidgeType" ;
ddlRidge.DataV alueField = "RidgeTypeI D";
ddlRidge.DataB ind();
}
----
This code successfully fills a DropDownList box. However, when theuser selects one of the items and the page is posted to the server,the SelectedItem property of the List is null and the SelectedIndex i(-1). I am tearing my hair out because I have found people on thenewsgroup with a similar
problem, but it was due to their not setting data source in the"this.PostBa ck =False" condition. I have done that, and it still doesnot help me.

I do have ViewState for the control set to True. I tried hardcodingthe ListItems in the HTML and the control values were read correctly.
I am sure I am missing the obvious here..but please help!!

Thanks,
Stacey
.

Nov 17 '05 #2
Stacey is not binding on the postback--at least not in the code below.
Notice the ! (not) in the IF statement. The code shown says to do the
binding if it's not a postback.

We need to see more of the code. What else happens if it IS a postback?
Do you manually re-populate the dropdownlist if it is a postback. If
that's the case, then that's the problem.

-bliz

--
Jim Blizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.
--------------------
Content-Class: urn:content-classes:message
From: "David Waz..." <dl*@pickpro.co m>
Sender: "David Waz..." <dl*@pickpro.co m>
References: <fd************ **************@ posting.google. com>
Subject: DataBound ListBox
Date: Wed, 2 Jul 2003 20:20:59 -0700
Lines: 57
Message-ID: <05************ *************** *@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNBEh+LwSBUfYK PT5OjrwgxMvdL4g ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
Path: cpmsftngxa09.ph x.gbl
Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:3192 6
NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet

You don't want to bind the data on post-back.
You are wiping out the results when you bind the data!

only bind when postback is TRUE, or when you know the
content will/should be changing. Otherwise, the viewstate
will handle everything else for you automatically.

email if you need more...
-----Original Message-----
I have the following code (this is a snippet):

InitDBConn( );
OpenDBConnect ion();

OleDbComman d cmdRidge = new OleDbCommand("S ELECT * FROM
TRDRidgeTypes ", oleDbConn);

OleDbDataRead er oleRidges= cmdRidge.Execut eReader();

if (!this.IsPostBa ck)
{
ddlRidge.Data Source = oleRidges;
ddlRidge.Data Member = "TRDRidgeTypes" ;
ddlRidge.Data TextField = "RidgeType" ;
ddlRidge.Data ValueField = "RidgeTypeI D";
ddlRidge.Data Bind();
}
----
This code successfully fills a DropDownList box. However,

when the
user selects one of the items and the page is posted to

the server
,the SelectedItem property of the List is null and the

SelectedInde x i
(-1). I am tearing my hair out because I have found

people on the
newsgroup with a similar
problem, but it was due to their not setting data source

in the
"this.PostBac k =False" condition. I have done that, and

it still does
not help me.

I do have ViewState for the control set to True. I tried

hardcoding
the ListItems in the HTML and the control values were

read correctly.

I am sure I am missing the obvious here..but please help!!

Thanks,
Stacey
.


Nov 17 '05 #3

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

Similar topics

0
1304
by: JB | last post by:
I have a databound listbox that is causing my form to hang when I call the show method. The list box is bound using the following code: _dataView.Table = _data.Tables; lboUsers.DataSource = _dataView; lboUsers.DisplayMember = "FullName"; then when I call form.Show(), the form shows up, but hangs before it draws any of the controls on the form. Is there any valid reason for this? Has anyone ever seen this before? I've written the same...
0
1276
by: Ken Varn | last post by:
I have a strange problem with a databound listbox. It may be because of how I use it, but I am stumped by this behavior. Here is the scenario: I have a multiselect databound listbox that belongs to a panel control. In the Page_Load event, the panel listbox control may be moved from one panel to another depending on selections that the operator makes. Everything seems to work fine except for one thing. The selections that are made in...
4
2310
by: Harold | last post by:
I have read the following article http://www.4guysfromrolla.com/webtech/073101-1.shtml. I added the empty selection as the first option. When I click submit without choosing anything it does not return my empty string but the 2nd value in listbox. When the page reloads it selects the 2nd option by default even thou my Empty field is in the list. What do I have to do to get the empty field and databound data to act as one?
1
1201
by: Brian Henry | last post by:
I am working on an data bound list that implements of course IList, IBindingList, IComparer, etc... your basic things your need for data binding type objects... the problem is i got it to work staticly... that is fill it with data then bind it and the data shows up in a listbox like it should... but if i was to add another object to the custom data bound list i made it doesnt show up in the listbox... how do you get this update to run...
2
3847
by: tangokilo | last post by:
Hello and thanks for your help, I have the following Listbox created in VisualStudio 2003 designer, desiring to select multiple entries from that list: ------------------------------- ListBoxUser.Location = New System.Drawing.Point(16, 240) ListBoxUser.Name = "ListBoxUser" ListBoxUser.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended ListBoxUser.Size = New System.Drawing.Size(136, 147) ListBoxUser.TabIndex = 6
3
1482
by: joe | last post by:
I actually have 2 questions: 1) Is databinding the fastest way to load a listbox from sqlserver? speed is crucial and I want to make sure i'm populating it the fastest way i can 2) Also, i'm having trouble getting the selected items in the listbox. Will simply using the Items property (with the index) not give me
1
1465
by: Gerwin Berentschot | last post by:
Hi all, I am looking for a databound listbox control that can be as easily used as the listbox control that comes with Ms Access (with as less programming as possible). I would like this listbox to have drag and drop capabilities, column and row format, etc. Does anybody know which control (commercial of non commercial) is suitable for my needs? Regards, Gerwin Berentschot
6
6658
by: =?Utf-8?B?SnVzdGlu?= | last post by:
Hello: Does anyone know how I can create a multi-column listbox in VB.Net (Windows)...I am using VS.Net 2003. If there is another control available that can be databound with multiple columns, I would be interested in looking into that as well. Here is the code I have (it cannot display multiple columns)...
3
1002
by: Scott McNair | last post by:
Hi, I have two listboxes (we'll call them LB1 and LB2). LB1 and LB2 are both populated from data, with the valuemember bound to the ID, and the displaymember bound to the data's text. I would like to be able to move the items back and forth between LB1 and LB2 by using "<-" and "->" buttons, so databinding is ruled out (since you can't add or remove items to a bound listbox).
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.