total newbie question about Listbox.selectedindex

T

ThunderMusic

Hi,
I just begun programming ASP.NET and I made a simple page with a listbox, a
label and a button. There are three items in the listbox, I want that when I
select an item, the label tells me which item is selected. I set the OnClick
event of the button to a sub with only "MyLabel.Text = MyList.SelectedIndex"
in it. And the SelectedIndex is always -1. Why? and how can I solve this?

Thanks
 
T

ThunderMusic

I don't think it's because if that, because my list is not databound. But I
post the code, so maybe you can see the problem:

' *********************Code Begins here ********************
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="StreamConfig.aspx.vb" Inherits="mywork.StreamConfig"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>mywork</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">

<script language="vb" runat="server">

Sub InitStreamList(Sender as Object, e as EventArgs)
StreamList.Items.Add("Item 1")
StreamList.Items.Add("Item 2")
StreamList.Items.Add("Item 3")
end sub

sub SubmitStream(byval Sender as object, byval e as EventArgs)
LBLResponse.text = GetSelectedStreamIndex
end sub

sub ShowSelectedItem(byval Sender as object, byval e as EventArgs)
LBLResponse.Text = StreamList.SelectedIndex
end sub
</script>

</head>
<body MS_POSITIONING="GridLayout">

<form id="StreamConfig" method="post" runat="server">
<asp:ListBox id="StreamList" OnLoad="InitStreamList"
OnSelectedIndexChanged="ShowSelectedItem" Runat=server />
<asp:Button ID="SeeConfig" Text="See Config" OnClick="SubmitStream"
Runat="server"/>

<asp:Label ID="LBLResponse" Runat="server" />
</form>
</body>
</html>
'***********************Code stops here******************

Nothing else is done. I'm I missing something (probably in fact)? Is it a
postback problem, a viewstate? something? I'm very new to all this. I did
some ASP 3.0, but it's completely different, so... And I'm used to
WindowsForms it doesn't seem to work the same way at all with WebForms.

Thanks for the help
 
K

Karl Seguin

Try:
Sub InitStreamList(Sender as Object, e as EventArgs)
if Not Page.IsPostBack then
StreamList.Items.Add("Item 1")
StreamList.Items.Add("Item 2")
StreamList.Items.Add("Item 3")
end if
end sub

Karl
 
T

ThunderMusic

it works. but I don't understand why. I did not remove the items, I just add
some. Why does it have an incidence?

thanks
 
K

Karl Seguin

InitStreamList is firing during postback BEFORE the event handler
ShowSelectedItem. This is causing your dropdownlist to have values re-added
to it. My guess is that it's defaulting to -1 because it simply no longer
knows which value to select. There are two "item 2"'s in there, so if you
select "item 2" how does it know which youw ant?

Karl
 
T

ThunderMusic

It's exactly because of the duplicated items thanks a lot for the
explanation and most of all, thanks for the help...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top