.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

listbox selected changed

Posted By:      Posted Date: October 04, 2010    Points: 0   Category :ASP.Net

Dear all,

Im new in MVC and trying to do some actions when the user selects  a item in a istbox.

the code of my listbox is:

 <%= Html.ListBox("ListBoxSelect", Model.ListBoxItems)%>


How can i do that ?


Thanks in advance.

View Complete Post

More Related Resource Links

ListBox Selected index Changed in a TabControl


Hello Folks,

again i need your help!

As you can see in the picture below i've got a Page control with inital letters which cotains a listbox showing different wood articles.
When i click on a Tab, the selected tab page get's a new Listbox control with the wood articels starting with the selected initial letter.

Creating the listbox:

 _woodList = (WC_WoodList)LoadControl(PageNames.WOODLIST);
            _woodList.InitialLetter = tabWoodLetters.ActiveTabPage.Text;

if (!String.IsNullOrEmpty(initialLetter))
                DataSet _temp = fuData.GetWoodSpecies(initialLetter);

                listboxGeneric.DataSource = _temp.Tables[0];
                listboxGeneric.ValueField = "BEZEICHNUNG";
                listboxGeneric.TextField = "BEZEICHNUNG";

                listboxGeneric.Height = _temp.Tables[0].Rows.Count * 19 + 3;


 When i click on a item inside the listbox, following code will be executed:

Trap Selected Index change event of listbox using Javascript.

Hi All, This is reagarding listbox selection. I have a list box with multi select option. I want to trap the selected index change event of Listbox through javascript. Item of list box can be selected using Click of mouse and also the keyboard up down key with shift. I tried onclick event to bind the javascript function. It works fine if I select by mouse, but it doesen't work for up/down key of keyboard. Thanks, Naveen

Fill a Listbox with value selected from a Dropdownlist

Hi, Maybe the question has already be posted, but I didn't find it.I have in my web page two controls: a dropdownlist and a listboxMy goal is to fill the listbox with values from my database depending on what I have selected in my dropdownlist.I wouldlike to use the CascadingDropDown component for that. Is that possible ??(I tried on my own and I get this error messageException Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ListBox' to type 'System.Web.UI.WebControls.DropDownList'.I think it's when i tried to fill the listbox with this function:[WebMethod] public CascadingDropDownNameValue[] GetAppliForDomain(string knownCategoryValues, string category) { int domainID; //this stringdictionary contains has table with key value pair of country and countryID StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); if (!kv.ContainsKey("Domain") || !Int32.TryParse(kv["Domain"], out domainID)) { throw new ArgumentException("Couldn't find Domain."); }; SqlConnection con = new SqlConnection(strConnection); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = System.Data.CommandType.Text

How can I make the listbox scroll to the selected item

Hi All, I have a textbox that searches the listbox below it and selects the text that I types in the textbox. Now although the item is selected in the listbox I have to scroll to the end to see if the item is selected ornot IS there a way that I can make the list box scroll to the selected item?    

ListBox.SelectedItem stuck at whatever is first selected and other strange ListBox behavior

Hi, I've built a List<Participant> (Participant being my custom type) and assigned this to ListBox.ItemsSource. (I'm aware this is more the Windows Forms way than the WPF way of databinding, but I'd still like to understand what's going on here - even though suggestions on how to perform my task in a more WPF-y way are also welcome.) I've set ListBox.SelectionMode to Single, and attached an event handler to the SelectionChanged event. In this handler, I dump the SelectedIndex and SelectedItem to debug output. Contrary to my expectation, 1) SelectedIndex is always -1. This isn't really causing me any trouble, but it's certainly not what I'd expect. 2) SelectedItem refers to the correct object the first time (in the windows lifetime) I select an item, but then keeps referring to this first-selected item regardless of whether I deselect (by clicking the item again while holding down the CTRL key) or select some other item. 3) If I keep clicking around and scrolling a bit (the list has ~320 items) within the list, now and then the list suddenly displays a bunch of items - sometimes many in a row, sometimes non-contiguous items, but it seems always "nearby" items! - as if they were selected. The list keeps firing the SelectionChanged event, but SelectedIndex is forever -1 and SelectedItem (and SelectedValue) always refers to whatever I had selected the fir

auto-scrolling to the selected item in a listbox



I have a listbox which is populated with items at Page_Load and has 5 items visible at a time.  Then there is a textbox where the user types some characters. On keyup event of the textbox  my code searches the listbox for the entered value and selects the item closest to the entered value. However the selected item appears as the 5th item in the listbox. I want it to be the 1st item displayed.

Any ideas ??


Adding Multiple Selected Values to ListBox Dynamically


I have a ListBox control that gets populated from a database like this:

For Each gvr As GridViewRow In GridView1.Rows
                Dim lb As ListBox = CType(gvr.FindControl("SectionsListBox"), ListBox)
                For Each sect As String In sectionlist


Then the selected values also get populated like this:

For Each gvr As GridViewRow In GridView1.Rows
                Dim lb As ListBox = CType(gvr.FindControl("SectionsListBox"), ListBox)
                Dim faqid As Integer = gvr.Cells(0).Text
                selectedlist = oCMS.getFAQSections(faqid)
                For Each sect As String In selectedlist
                    lb.SelectedValue += sect


I need to have multiple selections, but I cannot get it to select multiple values with this line "lb.SelectedValue += sect".  The listbox has been set in aspx to allow multiple selections.


Listbox selected record


Hi there,


I'm using VWD 2010 ASP.NET 4.0 SQL Server 2008 Dev.


I have list box with data binding to users table (FirstName + ' , ' + LastName)

Everything works fine except one thing (I have about 100 records)

Let say you click on user (5th row) number 5 this selected row moved up to the top of the list box


My question is how to prevent the selected record from moving up?

I wanted the record stay on his position



Oded Dror

How to handle treeview selected node changed event



From my below sample code, I don't want to use Sessions to handle each click event inside treeview selected node changed.

Secondly, if you suggest me to use treenode.SelectedValue then I would say my values are dyanmically generated.

Everytime a node a clicked, the selected node value is sent in the next request xml to get response and xml schema is different for each click. How do I handle this each click event other than using session variables. I thought of using Request.Item (and integrating with URL redirect property) but I don't want to use that.


Dim trNode As New TreeNode
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then        
            trNode.Text = "Not Postback"
            Session("Flag") = 1
        End If
    End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
        If Session.Item("Flag") = 1 Then
            trNode.Text = "Is PostBack"
            Session("Flag") = 2
        End If

The selected item in my listbox is always the previous item


It's easier if you see the code but I'll give you the specific problem. My listbox is set to contain a grid format and each listboxitem has a ListBoxItem.PreviewMouseLeftButtonDown event. The problem is, the selected item is always set to the previous item.

Here is what happens in steps:


1. My image loads up

2. I click a button to generate a grid overlay of 32x32 cells over the image, each a listboxitem in a cell of that overlay

3. I click on a cell 

4. OnSelected method fires (each listboxitem is tied to this handler through the ListBoxItem.PreviewMouseLeftButtonDown event)

5. The SelectedItem is null when I click on cell (0,0)

6. If I click on cell (0,1), the ListBoxItem.PreviewMouseLeftButtonDown event fires but now the SelectedItem is set to the object in the previous cell (0,0) instead of (0,1). Then, clicking on (0,0) will show the SelectedItem as being (0,1) and so on.

7. Repeat for any other cell.



<DockPanel Name="dockTest"

trouble displaying data from selected item in listbox


I have a asp.net web form in which I display registered customers in a listbox. In the listbox I can choose one customer, and click a "Show data"-button. That should display all registered data about the chosen customer in textboxes next to the listbox. This doesn't work and I haven't been able to find where the error lies. Instead of displaiyng the data in the textboxes the error message in:  LabelNoCustomerChosen is displayed. Could it be that the program doesn't find the customer ID? The string that is displayed in the listbox looks like this:

ID Lastname, Firstname

Here's my "Show data"-button click:

protected void ButtonCustData_Click(object sender, EventArgs e)

        string Firstname;
        string Lastname;
        string Street;
        string No;
        int Zip;
        string City;
        string Email;
        string Phone;

        if (ListBoxSearchCust.SelectedItem == null)
            LabelNoCustomerChosen.Visible = tru

How to unselect selected items in Listbox after SelectedIndexChanged


Hello i have a listbox LbGeneratedExcelLinks  when i click inside of it on an item it download the selected item

here s my code

Protected Sub LbGeneratedExcelLinks_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LbGeneratedExcelLinks.SelectedIndexChanged
        Dim RedirectUrl As String = LbGeneratedExcelLinks.SelectedValue
        LbGeneratedExcelLinks.SelectedIndex = Nothing
    End Sub

problem is if i want to click on another button or other event it keeps showing the dialog open or save file the one i put inside selectedindexChanged

 i need help to unselect after the selectedindexchanged i think it will solve my problem

C# listBox ScrollIntoView doesn't scroll to Selected Index


I have a WPF window that includes several ComboBoxes and ListBoxes. I have a button that is meant to reset the selected item in all the ComboBox and ListBox to the top item. This is designed to allow the User to "reset" their search parameters.

The programmatic selection of Index 0 works for all the boxes but the ListBox is presenting a problem. When I programmatically select index 0 in the ListBox it doesn't scroll to the selected Item. Because of this the user isn't immediately aware that the selection has been moved to the top item.

Here is the code I'm using:

private void button3_Click(object se

listbox displays value from selected radiobuttonlist


This is my problem.
I have a radiobuttonlist and listbox.
In my radiobuttonlist, i put datasource, displaying date. then i put a button.
What I want to do is:
I want to make the listbox displaying studentID & subjectID when I select date from radiobuttonlist and press the button.

ListBox Not Selected Validation?



I would like to know if its possible to validate a Listbox not selected? I tried to use RequiredFieldValidator but could not managed.

Thanks in advance.

Best Regards

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend