ListBox not showing correct selected value

Joined
Apr 10, 2007
Messages
2
Reaction score
0
Ok I have this problem that is driving me nuts! I am using this drop down's select index event to fire a method to populate this Listbox LbAccounts. Upon clicking the submit button I just want to retrieve the selected value or values. This is a multi selection listbox with view state enabled. I only bind this Listbox in the method shown below, and do not bind it again or touch it untill submitting the form. Whats driving me crazy is that sometimes it retains the selected value but sometimes it just gives me the first item in the listbox as the selected value when thats not it. I have done the exact same thing on other pages and it always gives me the correct selected item from the listbox. Could someone please show me what im doing wrong?? Your help is greatly appreciated

here is the code

// The event handler that populates the Listbox LbAccounts

protected void DDLSignatories_SelectedIndexChanged(object sender, EventArgs e)
{
GetAccounts();
}


protected void GetAccounts()
{

System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(cnnString);
SqlCommand cmd = new SqlCommand("sp_GetEntityAcctstoRemoveSigs", cnn);
cmd.Parameters.AddWithValue("@EID", DDLEntities.SelectedValue);
cmd.Parameters.AddWithValue("@SigID", DDLSignatories.SelectedValue);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable Accounts = new DataTable();
da.Fill(Accounts);
if (Accounts.Rows.Count == 0)
{
Acct.Style["display"] = "none";
lblNoAccounts.Visible = true;
return;
}
lblNoAccounts.Visible = false;
Acct.Style["display"] = "block";
LbAccounts.DataSource = Accounts;
LbAccounts.DataTextField = "Name";
LbAccounts.DataValueField = "Type_ID";
LbAccounts.DataBind();
LbAccounts.SelectedIndex = -1;
}

// and Finally the Submit button

protected void BtnSubmit_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(cnnString);

ArrayList accounts = new ArrayList();
foreach (ListItem li in LbAccounts.Items)
{
if (li.Selected == true)
{
accounts.Add(li);
}
}
}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top