Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox.SelectedValue always returns nothing

Status
Not open for further replies.

johnuk1

MIS
Aug 18, 2004
13
US
I have a asp.net page with a listbox, a textbox and a button. I simply populate the listbox on the page_load method with a couple of items. I then click an item however the textbox never gets populated. If i instead populate the textbox with the selectedindex of the listbox. In this case i always get -1 ie nothing selected.
in addition if i change the code behing the button to set the textbox go Listbox1.items.count it return 1 ie there is an item there!!!

why can i set another textbox to the correct number of items in the listbox but cannot reference the selected item directly?

 
here is the hhtml for the page

<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="test" enableViewState="False"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<link href="template.css" type="text/css" rel="stylesheet">
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout" bgColor="steelblue">
<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 40px" runat="server"
Width="300px" Height="120px" SelectionMode="Multiple"></asp:ListBox>
<asp:Button id="btEscalate" style="Z-INDEX: 102; LEFT: 88px; POSITION: absolute; TOP: 176px"
runat="server" Width="100px" Height="24px" Text="OK" CausesValidation="False"></asp:Button>
<asp:TextBox id="TextBox2" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 216px" runat="server"
Width="264px"></asp:TextBox>
<asp:Button id="btCancel" style="Z-INDEX: 104; LEFT: 200px; POSITION: absolute; TOP: 176px"
runat="server" Height="24px" Width="100px" Text="Cancel"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 0px; POSITION: absolute; TOP: 0px" runat="server"
Height="40px" Width="300px" BackColor="CornflowerBlue" Font-Bold="True">Escalate to Queue</asp:Label>
</form>
</body>
</HTML>
 
Heres how i handle multiple selection of email address to add to a cc field...
Code:
Dim item As ListItem
Dim strCC As String
   For Each item In ccLBox.Items
      If item.Selected Then
	strCC = strCC & item.Value & ";"
      End If
   Next item

its gotta be in your code, not your html
 
I believe there is also a SelectedItems property to a list box control.
Code:
Dim item As ListItem
Dim strCC As New System.Text.StringBuilder

  For Each item In ccLBox.SelectedItems
    strCC.append(item.Value & ";")
  Next item

  ccTextBox.Text =  strCC.ToString

see faq855-1882 [tt]String Concatenation in VB.NET [/tt]for why I chose a stringbuilder data type over a string data type

Jason Meckley
Database Analyst
WITF
 
why can i set another textbox to the correct number of items in the listbox but cannot reference the selected item directly?"

You're probably resetting the items each time in Page_Load, which resets the index.

If you have a line of code like:

myListBox.DataBind();

Make sure that you only populate it once by checking for !IsPostBack before binding:

if( !Page.IsPostBack )
{
myListBox.DataBind();
}

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top