Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my listbox has severals values. I am trying to pre select my listbox at page load but the problem is it only select the first value.
Suppose i have values in my listbox from 1-10 and in database column i have 1,4,7 then it pre select only 1 in the listbox.
If i have 1,2,7 then it preselect 1 and 2
If i have 1,2,3 then it preselect 1,2 and 3
So it pre select only together values if any gap occur then it select upper value.
ASP.NET
<asp:ListBox ID="dd_subcategory" runat="server" CssClass="lbcss" SelectionMode="Multiple">
</asp:ListBox>

C#
private void FillSubCategory(int CategoryID)
    {
        SqlCommand cmd = new SqlCommand("sps_bindcategory", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@flag", 2);
        cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            dd_subcategory.DataSource = objDs.Tables[0];
            dd_subcategory.DataTextField = "subcategory_name";
            dd_subcategory.DataValueField = "subcategory_id";
            dd_subcategory.DataBind();            
        }
    }

if (!Page.IsPostBack)
        {
            DataTable dt = GetData();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i]["subcategory_id"].ToString();
                    if (id == dd_subcategory.Items[i].Value)
                    {
                        dd_subcategory.Items[i].Selected = true;
                    }
                }
            }
        }
private DataTable GetData()
    {

        DataTable dt = new DataTable();
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("sps_addetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ad_id", ad_id);
            cmd.Parameters.AddWithValue("@useremail", ses);
            SqlDataAdapter DA = new SqlDataAdapter(cmd);
            DA.Fill(dt);
            con.Close();
        }
        catch (SqlException)
        {
            throw;
        }
        return dt;
    }

SQL
ALTER PROCEDURE [dbo].[sps_addetails] 
	@ad_id int,
	@useremail nvarchar(100)
AS
BEGIN
	select r.*, sc.subcategory_id from dbo.tbl_adregister r INNER JOIN tbl_adsubcategory sc on r.ad_id=sc.ad_id
	where useremail=@useremail and r.ad_id=@ad_id
END
Posted
Updated 9-Jun-14 21:44pm
v2
Comments
Kornfeld Eliyahu Peter 10-Jun-14 3:36am    
Can you show the markup of the listbox you use?
Raj Negi 10-Jun-14 3:46am    
sure, i added the markup and fuction which binds the list to the listbox.

1 solution

Your error is an assumption that the row index in the data set has any connection with the item index in the list...
Instead of using list.Items[i] you have to use list.FindByValue[^] to select the correct item...
C#
string id = dt.Rows[i]["subcategory_id"].ToString();
ListItem item = dd_subcategory.Items.FindByValue(id);

if(item!=null)
{
  item.Selected = true;
}
 
Share this answer
 
v2
Comments
Raj Negi 10-Jun-14 4:10am    
can you edit my code? how to apply that
Kornfeld Eliyahu Peter 10-Jun-14 4:24am    
see updated solution
Raj Negi 10-Jun-14 4:49am    
It works. thank you very much.
Kornfeld Eliyahu Peter 10-Jun-14 4:51am    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900