Problems selecting the selected item in a Listbox

Hi All,

I've got two listboxes which I want to allow a user to move items from one to the other. I do the population of the first list during the initial page load and bind the results to the first listbox. I have a button which does the move of the selected item to the second listbox.

The problem is when I select an item then click the move button it always moves the first item in the list even if that item is not selected, and never moves the selected item.

I've checked that during the postback the listbox is not being repopulated, and it isn't. Any help very much appreciated! thanks....and the code:

Pageload code: 

            //poopulate address grouplist
            if(!(IsPostBack))
            {
                #region
                try
                {
                   
                    //first time load org list
                    SqlCommand emailDtls = null;

                    DBConn = new SqlConnection(logConnect);

                    //select relevant stored proc
                    emailDtls = new SqlCommand("tp_getGroupEmailAddresses", DBConn);

                    //define the command type
                    emailDtls.CommandType = CommandType.StoredProcedure;
                    SqlParameter emailDtlsParam = emailDtls.Parameters.Add("@subscription_id", SqlDbType.Int);
                    emailDtlsParam.Value = Session["subscription_id"];

                    //open connection
                    DBConn.Open();

                    SqlDataReader emailDtlsReader = emailDtls.ExecuteReader();

                    //get details
                    if(emailDtlsReader.HasRows)
                    {
                        //populate message box
                        groupList.DataSource = emailDtlsReader;
                        groupList.DataTextField = "description";
                        groupList.DataValueField = "email";
                        groupList.DataBind();

                        //groupList.SelectedIndex = 0;

                        //enable buttons if page refreshed and buttons were disabled
                        if(!(addEmail.Enabled = true))
                        {
                            removeEmail.Enabled = true;
                            addEmail.Enabled = true;
                        }
                    }
                    else
                    {
                        //no group emails exsits disable select/remove buttons
                        removeEmail.Enabled = false;
                        addEmail.Enabled = false;

                        //display error message
                        RegisterStartupScript("Messaging Problem", "<script language=\"javascript\">alert('No Group emails have been set. Please report this to a System Administrator'); </script>");                       
                    }
                   

                    //close connection
                    DBConn.Close() ;

                }
                catch(Exception ex)
                {
                    //display message
                    UrlParameterPasser urlWrapper = new UrlParameterPasser("turinginformationsystem/errorPage.aspx");
                    urlWrapper["msg"] = ex.Message;
                    urlWrapper.PassParameters();
                }
                #endregion
            }
 

Button click code:

         private void addEmail_Click(object sender, System.EventArgs e)
        {
            bool hiLited = false;

            //check address is highlighted, if so move to
            if(groupList.SelectedIndex > -1)
            {
                //set indicator to show a record has been selected
                hiLited = true;
                
                //create entry in to list the delete from grouplist
                toList.Items.Add(new ListItem(groupList.SelectedItem.Text, groupList.SelectedValue));

                //delete item
                groupList.Items.RemoveAt(groupList.SelectedIndex);

            }

            
            //display message if nothing selected
            if(!(hiLited))
            {
                //display error message
                RegisterStartupScript("Messaging Problem", "<script language=\"javascript\">alert('Please select a group address first.'); </script>");                        
            }
        }

 

Regards

Davej 

 

 

0
flyingfishnm
3/6/2007 4:14:18 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

1 Replies
1099 Views

Similar Articles

[PageSpeed] 16

Dave:

I tested your code specially addEvent_Click its working fine.  I think there is some problem in your page load event, so please double check your code.

Thanks


Girijesh
http://www.girijesh.in/
0
Girijesh
3/6/2007 6:36:11 PM
Reply:

Similar Artilces:

Why is the select saying selected after an item is selected?
This was a whitelisting issue due to a software upgrade at hostgator..  Nothing wrong with my code..Scot King SEO Software Tool | Search Engine Optimization"Get your page rank high"  huh? It is smart to put your question in the body and not just in the title. Have no idea what your question is or if it even a question.Eric Pascarello.com | Twitter epascarello | LinkedIn use thisddl.options[ddl.selectedIndex].selected = false; //Clear the selection ddl.options[1].selected = trueMAKMark as Answer if this reply helps youMVP ASP/ASP.NetASP.Net Hosting : Host DepotMy Sit...

Load Text box of one web form with selected list item of list box of another web form
one help please Problem is i have two web forms  1.  ForiegnkeyTest.aspx  2. foroginkeytable.aspx   step 1 1 ForiegnkeyTest.aspx one web form <head runat="server"><script language="javascript" type="text/javascript">   function popWin()   {         var confirmWin = null;      confirmWin = window.open('foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

Deleting all selected items in a multiple-select listbox
I am trying to write a simple loop that is called on a button click event so that any selected items in my multiple-select mode listbox are removed. I cannot seem to get it to work and I would be grateful for help. The code below only removes one item when I use it, even if more than one item is selected in the listbox. Many thanks. 1 2 Dim i As Integer 3 For i = ListBox1.Items.Count - 1 To 0 Step -1 4 If ListBox1.Items(i).Selected Then 5 ListBox1.Items.RemoveAt(i) 6 End If 7 Next    ...

syntax: if last selected item in listbox is and is/not selected??
Hi i currently have this event behind a button, but i want to add another variable 'strNames' that carries out the same behaviour as 'strValues' but i want it to insert the word 'and' between the two last selected items in the listbox aswell as adding a fullstop after the last item. I am not sure what the c# code is to say 'not last selected item' either.....?? So far i have...(but not sure if on the right lines).   try{ string strValues = ""; string strNames = ""; foreach (ListItem item in authorSelectionListbox.Items){ i...

Selecting Items in a multiple selection ListBox that is in a DataGrid?
Is is possible to select multiple items of a listbox that is in the EditItemTemplate of a DataGrid? When someone goes to the edit button on a DataGrid (using the standard OnEditCommand for editing) I want there to be a multiple selection listbox that already has the stored values selected. I have this working for dropdownlist but I cannot get it to work for multiple selection listbox. I am not sure where to put the code that does the selections, I tried to put them on the OnEditCommand but when I use e.Item.FindContol it doesn't find the listbox. Any help would be great! Hi, Can you po...

ListBoxe Selected Item problem
Hello, in one of my aspx-pages, I bind a listbox to a dataset. The correct data is displayed in the listbox but if I use the selecteditem property, I get a NullReferenceException. I think my index does not change. Here's my code: in the page_load methode: daProfiles.Fill(DsProfiles, "t_Chat_Senders") lstProfiles.DataSource = DsProfiles lstProfiles.DataTextField = "name" lstProfiles.DataValueField = "MSISDN" lstProfiles.DataBind() Private Sub btnProfile_Click(ByVal sender As ...

Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

ListBox selected index change problem with selected value
I have a problem with my list box selected index changed. There are two items in the list box one is a Group Name and then below each group are email addresses that go with those groups. When I select the group name it puts the selected value where it is equal to group_id  the select statement looks like this and works correctly: SELECT d.group_descr, a.first_name, a.last_name, a.user_id, b.email, d.group_id, d.parent_group_id FROM icc_user_data AS a LEFT OUTER JOIN icc_users AS b ON a.user_id = b.user_id LEFT OUTER JOIN icc_users_groups AS c ON a.user_id = c.user_id LEFT OUTER JOIN ...

Can you Unselect a Selected Item in a Single Select Listbox
I have a very simple single select listbox. Once a user selects an item, is there any way to unselect that item without selecting another item. Do I have to put a None option in the box so if someone accidentally selects an item and does not want an item they can then select None?Thanks.Cliff------ private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) {//do your work with the selected index change //now reset selected index back to -1ListBox lb = (ListBox)sender;lb.SelectedIndex = -1;}Answering a question increases your knowledge asking a question shows your Inte...

ListBox selected item moves to top of selection on repost
I have a ListBox with approximately 80 items.  When I scroll down, make a selection - the selected item appears in the first position on repost.  I am told this in not normal.The ListBox Items are all unique but there are instances where different Items have the same Value - see partial example below.  Would this condition cause the behavior I am seeing? Thomas Sun - MSFT  commented the following in an earlier post.  "Did the ListItem have the same DataValue? If so, The ListBox will select the first Item, the DataValue of which matchs selected va...

How to find selected index and total no. of selected items in a listbox
helloI am working in asp.net 2.0 and visual C# 2005I am selecting multiple items in a list box.Now I want a count of the seleted items also the index of the selected itemI browsed and found something called" selectedindices.count "but unfourtunately when i type listbox1 the selectedindices does not popup. I did write ListBox1. but all i could trace wasselectedindexselectedindexchangedselectedvalueselecteditemselectionmodeselectedindices seems nowhere in the listAny help please thanks and regards cmrhema Dim i As Integer = 0Dim cnt As Integer = 0 For i=0 To ListBox1....

superreview granted: [Bug 112281] ctrl+shift+click whle making range selections in form <select> shouldn't wipe out other selected items : [Attachment 135533] Proposed patch
Robert O'Callahan <roc@ocallahan.org> has granted Boris Zbarsky (real world intrudes) <bz-vacation@mit.edu>'s request for superreview: Bug 112281: ctrl+shift+click whle making range selections in form <select> shouldn't wipe out other selected items http://bugzilla.mozilla.org/show_bug.cgi?id=112281 Attachment 135533: Proposed patch http://bugzilla.mozilla.org/attachment.cgi?id=135533&action=edit ------- Additional Comments from Robert O'Callahan <roc@ocallahan.org> looks quite straighforward :-) ...

superreview requested: [Bug 112281] ctrl+shift+click whle making range selections in form <select> shouldn't wipe out other selected items : [Attachment 135533] Proposed patch
Boris Zbarsky (real world intrudes) <bz-vacation@mit.edu> has asked Robert O'Callahan <roc@ocallahan.org> for superreview: Bug 112281: ctrl+shift+click whle making range selections in form <select> shouldn't wipe out other selected items http://bugzilla.mozilla.org/show_bug.cgi?id=112281 Attachment 135533: Proposed patch http://bugzilla.mozilla.org/attachment.cgi?id=135533&action=edit ------- Additional Comments from Boris Zbarsky (real world intrudes) <bz-vacation@mit.edu> roc, what do you think? ...

SELECT from SELECT
Hi! I remember seeing an example where you can do a select from a nested select, but I cannot seem to make it work. So it would be something like this: select max( name ) from ( select name from customer union select name from vendor ) Was I dreaming I saw that? Thanks! D In article <4500243f@forums-2-dub>, daniel_AT_infrontsoftware_DOT_com@N0SPAM.com says... > Hi! > > I remember seeing an example where you can do a select from a nested > select, but I cannot seem to make it work. No, you weren't. Try (UNTESTED!) adding a...

Web resources about - Problems selecting the selected item in a Listbox - asp.net.web-forms

Consultation Document on the Methods for Selecting the Chief Executive and for Forming the LegCo in 2012 ...
The " Consultation Document on the Methods for Selecting the Chief Executive and for Forming the LegCo in 2012 " was a document published on ...

Voting Machine Caught on Camera Casting Ballot for Democrat when Selecting Republican - YouTube
**UPDATE.... I've heard many reports that this is "just a calibration issue." I will say this... when I went to page 2 and began voting for city ...

Selecting decor that will move with the times
What colours should I use in my home so that my d&#233;cor is neutral and flexible but not boring?

Clock ticking on selecting successor to Leigh Clifford at Qantas
With Qantas on course to chalk up its largest annual profit since 2008, the question now is whether chairman Leigh Clifford is looking to hang ...

More to selecting political background music than indiscriminate meandering
Gathering a song to use as a protest weapon can backfire.

Jackie French: Why selecting the right place for your plants is important
The secret of growing anything easily is to choose the best spot for what you want to grow.

Liberals begin 'super weekend' of selecting delegates
A race for the top job in the federal Liberal party gained momentum on Friday as rank and file party members began the selection process for ...

Boston Marathon bombing trial: Judge, lawyers face challenges in selecting jurors - CTV News
As the second phase of jury selection began in the federal death penalty trial of Boston Marathon bombing suspect Dzhokhar Tsarnaev, it immediately ...

Selecting cabinet one of first orders of business for Trudeau
One of the first orders of business for any new prime minister is picking a cabinet. Who is likely to make the cut?

Agile Analysis, Self-Selecting Teams, TDD & BDD in Methods & Tools Winter 2014 issue
... for software developers, testers and project managers – has just published its Winter 2014 issue that discusses Agile Analysis, Self-Selecting ...

Resources last updated: 11/29/2015 11:15:43 AM