473,614 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a Default ListItem in a Databound Listbox in ASP.NET- Postback

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?
Nov 19 '05 #1
4 2300
In your Page Load - around the BindData section - put an if/then/postback
block:
if not Page.IsPostback then
BindData
end if

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx


"Harold" <re***@togroup. com> wrote in message
news:en******** ******@TK2MSFTN GP14.phx.gbl...
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?

Nov 19 '05 #2
Harold wrote:
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?


After you insert the first item, you could do:
dropDownList.It ems[0].Selected = true;

thus ensuring the first item is the selected item.
--
Scott
Nov 19 '05 #3
I have the if statement and it still gives me the same results. When I
debug the code ddlcatcode.Item s(0).text = "" when the page loads. When I
press the search button I get ddlcatcode.Item s(0).text = "13120" and I
should get ddlcatcode.Item s(0).text = "" because I didn't choose anything
from that listbox. When the form is posted back it seems to loose the
dynamic listitem I added.
"David Wier" <dw***@nospamAS PNet101.com> wrote in message
news:Oh******** ******@TK2MSFTN GP09.phx.gbl...
In your Page Load - around the BindData section - put an if/then/postback
block:
if not Page.IsPostback then
BindData
end if

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx


"Harold" <re***@togroup. com> wrote in message
news:en******** ******@TK2MSFTN GP14.phx.gbl...
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?


Nov 19 '05 #4
I am now doing this and it seems to be working. Not sure if this is how it
should be done or not.

If Not Page.IsPostBack Then

GetData(strSQL)

Else

ddlCatCode.Item s.Insert(0, New ListItem("", ""))

End If

Private Sub Page_PreRenderC omplete(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.PreRenderCom plete

'Add an empty row to catcode dropdownlist

If Not Page.IsPostBack Then

ddlCatCode.Item s.Insert(0, New ListItem("", ""))

End If

End Sub

"Harold" <re***@togroup. com> wrote in message
news:uc******** ******@tk2msftn gp13.phx.gbl...
I have the if statement and it still gives me the same results. When I
debug the code ddlcatcode.Item s(0).text = "" when the page loads. When I
press the search button I get ddlcatcode.Item s(0).text = "13120" and I
should get ddlcatcode.Item s(0).text = "" because I didn't choose anything
from that listbox. When the form is posted back it seems to loose the
dynamic listitem I added.
"David Wier" <dw***@nospamAS PNet101.com> wrote in message
news:Oh******** ******@TK2MSFTN GP09.phx.gbl...
In your Page Load - around the BindData section - put an if/then/postback
block:
if not Page.IsPostback then
BindData
end if

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx


"Harold" <re***@togroup. com> wrote in message
news:en******** ******@TK2MSFTN GP14.phx.gbl...
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?



Nov 19 '05 #5

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

Similar topics

7
17751
by: Amil | last post by:
Hi all, I am using VS 2005 Beta 2. Which assembly (namespace?) should I use for the ListItem class? I don't have the Help files installed on my notebook for space reasons; I use MSDN2 online instead. I see ListItem as derived from System.Web.UI.Controls namespace. Am I missing something here? I am creating a Windows application by the way.
4
2893
by: Hong Yip | last post by:
Hi Everyone, I have an array of checkboxlist. Everything works prefectly fine until I start working on the "presentation" view. I am not able to "insert" a checkbox item to a table cell. Something like that, TableCell TCell = new TableCell();
1
1480
by: Ami Schreiber | last post by:
I want to insert a default null ListItem with the text "choose an item ..." in my DropDownList control. I have seen similar postings here regarding this issue but all of them seem to focus around having to populate datasources, datatables etc. and then bind the DropDownList in the codebehind of the .aspx page. I am using Visual Studio 2005 and the .NET Framework 2.0. I thought that with all these new improvements we could avoid all of the...
6
3655
by: Papa.Coen | last post by:
I've just spend a lot of time solving the following problem: I used dotNet 2.0 / Visualstudio 2005 / C# / aspx, enable viewstate is set on all controls. I have 2 listboxes; the left contains items that can be (dynamically) added to the right box. Adding the first item went fine, it shows up in the right box. Adding the second item resulted in losing _every_ control following the right box. 'View source' reveiled that the rendering of the...
15
3094
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
4
4670
by: Kevin Blount | last post by:
As mentioned before, I'm creating a multi-lingual page where the text of the page comes from a database. This page includes a registration form which asks for address information, including the Country. I have 6 .txt files that contain a complete list of countries that we sell to (65 in total), each written in the appropriate way for the language of the site, e.g. Mexico for English US and UK sites and México for the Spanish language...
1
2190
by: MaRkHaSBEEnMade | last post by:
Hi there I currently have a listbox that displays provinces,regions and suburbs.I want to pad the items logically with spaces,but the spaces are being igonred... eg Province1 Regions1 region2 Province2 Region3
1
3177
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems collection, Text and Value. I need to add a DivisionID, and DepartmentID. I assumed i could simply override the ListItem class and add the additional properties. is this in fact the case? If so what would the code look like?
3
3160
by: dizzy77 | last post by:
I have a dropdownlist databound to table column with string values "n" i "y". <asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'> <asp:ListItem Value="n">No</asp:ListItem> <asp:ListItem Value="y">Yes</asp:ListItem> </asp:DropDownList> How to handle situation when value is not "n" or "y" to avoid exception. Is it possible to make dropdownlist select some default value...
5
26627
by: nzkks | last post by:
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005, SQL Server 2005, Formview controls In a ASP.Net form I have 20 textboxes and 20 dropdownlists(ddl). All ddl(s) are databound and get the data from a single objectdatasource. All textboxes and ddl(s) support null values. Textboxes are for entering numbers and ddl(s) are for selecting the unit (mm, cm, in, m3, oz, qt like these.). I am using the below code to achieve two...
0
8120
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
8620
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...
0
8571
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8423
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...
1
6085
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
5537
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2560
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1420
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.