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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't find selected items in a listbox

Status
Not open for further replies.

timothy

Programmer
Mar 20, 2000
39
US
I have a listbox that I populate it from the database; then on submit check for selected items. It always returns false (not selected). However if I hardcode the values into the listbox I do find the correct selected values.

Any idea why this would be?

Code:
private void Page_Load(object sender, System.EventArgs e){
dba = new DBA.DBAccessor();
dba.ConnStr = @"Data Source=someDS;User ID=someID;Password=somePWD;";
sql = "SELECT GID, DESC FROM SOME_GROUPS";
ds = dba.GetOraDataSet(sql,"Groups");
lstRoles.DataSource=ds.Tables["Groups"];
lstRoles.DataTextField="DESC";
lstRoles.DataValueField="GID";
lstRoles.DataBind();		
}

private void Button1_Click(object sender, System.EventArgs e){
  ArrayList al = GetSelectedItems(lstRoles);
  // do something with the array "al"
}

private ArrayList GetSelectedItems(ListBox listBox){
ArrayList returnVal = new ArrayList();
foreach(ListItem item in listBox.Items)
  if(item.Selected) returnVal.Add(item.Value);

return returnVal;
}

Thanks for your thoughts!
 
Bind your data under the condition,

if (!Page.IsPostBack)
BindData();

in your Page_Load() event handler.

HTH,
Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top