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);
}
}
}
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);
}
}
}