Advertisement
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";
}
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";
}
Advertisement
Advertisement
-
Re: Need a little help
Mon, November 14, 2005 - 11:36 AMFirstly, i'd suggest wrapping the code you have in the Page_Load in a check for if(!Page.IsPostBack){}, that way you will only databind once. Secondly, I tried this on a page and could not recreate. This is all i have:
<asp:ListBox ID="lsbTest" Runat="server" AutoPostBack="True"></asp:ListBox>
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
for(int i=0;i<5;i++)
{
ListItem item = new System.Web.UI.WebControls.ListItem("bob" + i.ToString(), i.ToString());
lsbTest.Items.Add(item);
}
// Put user code to initialize the page here
}
private void lsbTest_SelectedIndexChanged(object sender, System.EventArgs e)
{
Response.Write(lsbTest.SelectedIndex);
}
and it's (0 - giving me the value of the item that i did click.
See if you can get this to work and maybe go from there? -
-
Re: Need a little help
Mon, November 14, 2005 - 11:38 AMalso, i know i left it out because visual studio normally does this automatically, but i just remembered that sometimes i'll reopen a project that i haven't tried in a while, and the event bindings that were there will have disappeared, so i'd suggest making sure that your InitializeComponent method is getting called and you have something like this in there;
this.lsbTest.SelectedIndexChanged += new System.EventHandler(this.lsbTest_SelectedIndexChanged);
-