Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to design an edit page in a web application. I have a listbox in my form which has selected items stored in my database. Now I want to have them selected on page_load in my listbox. I tried item.selected = true, but it doesn't work. and I have no more idea how to do this
Posted
Comments
Ajith80301 12-Oct-10 7:38am    
Can you tell how did you save those selected items in the database?? Then it would be easier to suggest a solution..
Brij 12-Oct-10 9:30am    
Can you post your code? It will help us to answer better
Maziyarkh 13-Oct-10 12:50pm    
I stored the values in a database, but I used several classes and tables and ... so it just complicates the situation and I have to post the whole project. But I understood something, if the ListBox is static with certain items, this code will select all fields on page load:

foreach (ListItem index in StaticListBox.Items)
{
index.Selected = true;
}
which is written in page_load, but if the listbox is dynamic (which is mine), the same code does'nt work.
so the first step and first question is how to select all items of a listbox?

create table employee(
ID int,
name nvarchar (10),
salary int,
start_date datetime,
city nvarchar (10),
region char (1))
Let us suppose this is our table .I am giving the code that you need to use.
make a method as in the code behind of page.
void bindlist()
{
SqlCommand cmd = new SqlCommand("select * from employee", con);
con.Open();
SqlDataReader dtr = cmd.ExecuteReader();
ListBox1.DataSource = dtr;
ListBox1.DataTextField = "name";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
con.Close();

}
and call it in as
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindlist();

}
 
Share this answer
 
v2
Comments
m@dhu 12-Oct-10 9:29am    
The question was not to populate the listbox but to retrieve the selected values to listbox from database.
Mohd Wasif 13-Oct-10 1:28am    
Give proper question?
Or u want to say that u have 2 listbox and First List box Selected Items U want to Show in another list box on pageload.
Or Something else?
Maziyarkh 13-Oct-10 6:32am    
I think I know whats the problem, but I dont know the solution! The problem is when you have a dynamic listbox binded to a database, the items cant be selected on page load, I described what I mean in answer3.
Listbox1.SelectedValue=//value from db
 
Share this answer
 
thanks for your response, but it's not the solution. lets make my quesyion easier. Noe I just want to select all items in a ListBox which gets its Items from a database.
if the ListBox is static with certain items, this code will select all fields on page load:
<pre lang="cs">foreach (ListItem index in StaticListBox.Items)
{
index.Selected = true;
}</pre>
which is written in page_load, but if the listbox is dynamic (which is mine), the same code does'nt work. I can bind my ListBox to a database, but I can't select them with code.
 
Share this answer
 
Comments
kingsa 14-Apr-12 3:12am    
hi i have got same problem selected item how do i get can u guide it important for me

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