473,414 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

DataBound ListBox

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

InitDBConn();
OpenDBConnection();

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

OleDbDataReader oleRidges= cmdRidge.ExecuteReader();

if (!this.IsPostBack)
{
ddlRidge.DataSource = oleRidges;
ddlRidge.DataMember = "TRDRidgeTypes";
ddlRidge.DataTextField = "RidgeType";
ddlRidge.DataValueField = "RidgeTypeID";
ddlRidge.DataBind();
}
----
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.PostBack =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 4729
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();
OpenDBConnection();

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

OleDbDataReader oleRidges= cmdRidge.ExecuteReader();

if (!this.IsPostBack)
{
ddlRidge.DataSource = oleRidges;
ddlRidge.DataMember = "TRDRidgeTypes";
ddlRidge.DataTextField = "RidgeType";
ddlRidge.DataValueField = "RidgeTypeID";
ddlRidge.DataBind();
}
----
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.PostBack =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.com>
Sender: "David Waz..." <dl*@pickpro.com>
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+LwSBUfYKPT5OjrwgxMvdL4g==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa09.phx.gbl
Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.framework.aspnet:31926
NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
X-Tomcat-NG: microsoft.public.dotnet.framework.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();
OpenDBConnection();

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

OleDbDataReader oleRidges= cmdRidge.ExecuteReader();

if (!this.IsPostBack)
{
ddlRidge.DataSource = oleRidges;
ddlRidge.DataMember = "TRDRidgeTypes";
ddlRidge.DataTextField = "RidgeType";
ddlRidge.DataValueField = "RidgeTypeID";
ddlRidge.DataBind();
}
----
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.PostBack =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
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 =...
0
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...
4
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...
1
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...
2
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: -------------------------------...
3
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,...
1
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...
6
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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...
0
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...
0
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...

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.