Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a listbox in which three list items have been hard coded.
<td align="justify">
                            <asp:ListBox ID="ListBox1" Width="200" SelectionMode="Multiple" runat="server" >
                            <asp:ListItem Text= "SP" Value= "1">
                            <asp:ListItem Text= "SC" Value= "2" >
                            <asp:ListItem Text= "TA" Value= "4" >       
                   
                           
                        </td>

I intend to keep the selected item selected(highlighted) when i check it again.
Please guide how can it be done.
Posted
Updated 14-Apr-11 0:04am
v3
Comments
BobJanova 14-Apr-11 6:05am    
Edited to add ASP.net tag.

<asp:ListItem Text="SP" Value= "1" Selected="true" ></asp:ListItem>


Update :

Before leaving the page, you can store your selected value in a Session.
Session["SelectedListItem"] = ListBox1.SelectedValue;


And on your PageLoad, you can check the Session["SelectedListItem"] if it has value and set it.
if (Session["SelectedListItem"] != null) {
   ListBox1.SelectedValue = (string)Session["SelectedListItem"];
}
 
Share this answer
 
v2
Comments
Aksh@169 14-Apr-11 5:46am    
I want that when i come back to the page where i selected the items in the listbox, i should find them selected(highlighted) still
Pong D. Panda 14-Apr-11 6:09am    
check my updated solution
I'm not an ASPX expert but ... I think you will need to store the information in the session, and in your Page_Load handler for this page, set the appropriate items to be selected again. Remember that HTTP is by default stateless and so each visit to the page is a new visit, unless you write some session code to remember things.
 
Share this answer
 
SQL
ListBox.Items.Add(New ListItem("Select ListBox Type", ""))
        ListBox.DataSource = object
        ListBox.DataValueField = "TextField"
        ListBox.DataTextField = "ValueField"
        ListBox.DataBind()
        ListBox.SelectedIndex = 0


Here it Shows by Default selection as "Select ListBox Type" bcz it's SelectedIndex is set to ZERO(0).
You can change as per your requirement.
 
Share this answer
 

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