This is a classic case of the PostBack Paradox.
The Page_load event gets called EVERYTIME you post back.
Currently your code reloads the list box when you change the selected
index, wiping out the selection change. Try this instead
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ListBox1.DataSource = LoadGames();
ListBox1.DataTextField = "GameName";
ListBox1.DataValueField = "GameNumber";
ListBox1.DataBind();
}
}
On 11/10/05, teget1024 <[EMAIL PROTECTED]> wrote:
> I am working on an application where I want to populate a list, have
> teh user select one of the items and then act on that selection. I
> have created a list box and I populated it with text and values, but
> when I select an item from the list, the selected index does not
> change. I have tried multiple times, but each time the selected index
> does not change. I will post the list box definition and the c# code
> I am using below. I have gotten the code to execute the
> ListBox1_SelectedIndexChanged method, but the ListBox1.selectedIndex
> is always -1. Any help would be appreciated.
>
> ListBox Definition:
>
> <asp:ListBox id="ListBox1" style="Z-INDEX: 101; LEFT: 184px; POSITION:
> absolute; TOP: 168px"
> runat="server" Width="336px"
> AutoPostBack="True"></asp:ListBox>
>
>
> c# code:
>
> private void Page_Load(object sender, System.EventArgs e)
> {
>
> ListBox1.DataSource = LoadGames();
> ListBox1.DataTextField = "GameName";
> ListBox1.DataValueField = "GameNumber";
> ListBox1.DataBind();
> }
>
>
> public SqlDataReader LoadGames()
> {
> SqlCommand myCommand;
> SqlDataReader myDataReader;
>
> sqlConnection1.Open();
> //prepare sql statements
> myCommand = new SqlCommand("SELECT * FROM Games WHERE
> NumberOfPlayers < MaxPlayers", sqlConnection1);
> myDataReader = myCommand.ExecuteReader(
> CommandBehavior.CloseConnection);
>
> return myDataReader;
> }
>
> private void ListBox1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
>
> Label1.Text = "Fired";
> }
>
>
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
--
Dean Fiala
Very Practical Software, Inc
http://www.vpsw.com
------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/A77XvD/vlQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/