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

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

Listbox selected record

Posted By:      Posted Date: September 25, 2010    Points: 0   Category :ASP.Net

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

View Complete Post

More Related Resource Links

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

Print a specific Access Report with selected GridView record

I have my code output a gridview with all the data with an option to select records. I'm trying to make a code which would allow me to select a record, and then print this record using a specific Report stored in MS Access. I've located the code to print the Report it self, but it prints ether all the records or a range. Any ideas? 

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.


select record based on / match selected date

with the sql statement below, i try to select the records based on the selected date, it return no rows from adapter filled datatable. 
however, when i run the query in sql query in sql server studio management, it returns records. 
date format = 09/28/2010

string[] splitDT = txtSelectedDate.Text.Split('/');

            string strRearrangeDT = splitDT[0] + "/" + splitDT[1] + "/" + splitDT[2];

using (SqlConnection sqlconn = new SqlConnection(connstring))
                DataTable dt = new DataTable();
                string strSQL = "SELECT userclaim_details3.id, userclaim_details3.userclaimid, userclaim_details3.userid, userclaim_details3.childcategorytableid, userclaim_details3.statusid, " +
                         "userclaim_details3.amount AS amountname, userclaim_details3.remark AS remarkname, userclaim_details3.datecreated AS datecreatedname, " +
                         "userclaim_details3.datemodified, approvaltable.id AS Expr1, approvaltable.typename AS statusname, usertable2.id AS Expr2, usertable2.name AS uname, " +
                         "childcategorytable.id AS Expr3, childcategorytable.categor

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"

listbox selected changed


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.

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

Dropdown List in Gridview - While saving the record in gridview the droppdown selected index are cha



I am developing page for maintaing employee record using Visual Studio 2008, MS SQL server 2005.

I am able to fetch and display all the employee information in gridview, the gridview have the dropdown list to display the employee type [ Part Time, Full Time, Permanent , Contract ], in the item template i am having dropdown list. IN the dropdown list i am displaying this.

But While saving the record in gridview the droppdown selected index are changed to 0 of other dropdown list.

the user may be change the dropdown value from permanent to contract, or so. but while click on the save button in gridview, all the previous selection are gone, and always showing the first record of dropdown list. I know this is happening because we are doing dropdown list bind on onRowDataBound.

But i want to retain all the user selection in gridview, please help me how to do this.

thanks in advance.

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

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:

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

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