Solved

Multi Select LIST-BOX problem

Posted on 2004-09-28
6
303 Views
Last Modified: 2008-02-01
Hi,

I'm just trying to use a multi-select list box:
Data from a database is read into the ListBox.Value and ListBox.Text fields, the user can then select multiple rows holding down the ctrl button. This all works fine. The problem is I can't detect in the code what choices the user has made.

If ItemA.Selected is always false even if it is a selected row.
-------------------------
Protected WithEvents SelectionList1 As System.Web.UI.MobileControls.SelectionList         'the list box is declared in code as you can see

Private Sub DetectChoiceMade()                          'As you can see there's nothing wrong with the code (I think anyway)
        Dim ItemA As MobileControls.MobileListItem   'code does loop so many times as there are rows but ItemA.Selecte is always FALSE for
        For Each ItemA In SelectionList1.Items           'some reason. Therefore the body of the If statement is never entered.
            If ItemA.Selected Then
                Command1.Text = ItemA.Text
            End If
        Next
End Sub
-------------------------
0
Comment
Question by:Artform04
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:Artform04
ID: 12167777
Im sure somebody must know the solution or at least had a simillar problem with a mobile:selectionList (listbox).
0
 
LVL 1

Author Comment

by:Artform04
ID: 12168216
come one guys, I'm giving away 500 points here!

It crossed my mind the selected fields could get deselected for some reason??? someone come on please
kind regards.
Martin
0
 
LVL 5

Expert Comment

by:stumpy1
ID: 12168382
I got this from the MSDN help ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemwebuiwebcontrolslistboxclassselectionmodetopic.htm

Its C# but simple enough that you shouldnt have any problem converting it and splitting it into HTML and CodeBehind.

<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>

   <script runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e)
      {

         Message.Text = "You chose: <br>";
         
         // Iterate through the Items collection of the ListBox and
         // display the selected items.
         foreach (ListItem item in ListBox1.Items)
         {

            if(item.Selected)
            {

               Message.Text += item.Text + "<br>";

            }

         }

      }

      void Selection_Change(Object sender, EventArgs e)
      {

         // A single-selection ListBox cannot have multiple items selected.
         // Make sure only one item is selected before changing selection modes.
         ListBox1.SelectedIndex = 0;

         // Set the selection mode.
         ListBox1.SelectionMode = (ListSelectionMode)SelectionModeList.SelectedIndex;

      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form runat=server>

      Select items from the list and click Submit. <br>
      Choose the selection mode from the drop-down list. <br>

      <asp:ListBox id="ListBox1"
           Rows="6"
           Width="100px"
           SelectionMode="Single"
           runat="server">

         <asp:ListItem Selected="True">Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
         <asp:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>

      </asp:ListBox>

      <br><br>

      Selection mode:

      <br>

      <asp:DropDownList id="SelectionModeList"
           AutoPostBack="True"
           OnSelectedIndexChanged="Selection_Change"
           runat="server">

         <asp:ListItem Selected="True">Single</asp:ListItem>
         <asp:ListItem>Multiple</asp:ListItem>

      </asp:DropDownList>

      <br><br>


      <asp:button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click"
           runat="server" />

      <br><br>
       
      <asp:Label id="Message"
           runat="server"/>
       
   </form>

</body>
</html>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Accepted Solution

by:
stumpy1 earned 500 total points
ID: 12168465
Here is the HTML and C# code split up. Ive checked this and it works. Just create a new page and replace everything in the HTML between <HTML> and </HTML> with the following (leave and <%@ directives).

<HTML>
      <HEAD>
      </HEAD>
      <body>
            <h3>ListBox Example</h3>
            <form runat="server" ID="Form2">
                  Select items from the list and click Submit.
                  <br>
                  Choose the selection mode from the drop-down list.
                  <br>
                  <asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Single" runat="server">
                        <asp:ListItem Value="Item 1" Selected="True">Item 1</asp:ListItem>
                        <asp:ListItem Value="Item 2">Item 2</asp:ListItem>
                        <asp:ListItem Value="Item 3">Item 3</asp:ListItem>
                        <asp:ListItem Value="Item 4">Item 4</asp:ListItem>
                        <asp:ListItem Value="Item 5">Item 5</asp:ListItem>
                        <asp:ListItem Value="Item 6">Item 6</asp:ListItem>
                  </asp:ListBox>
                  <br>
                  <br>
                  Selection mode:
                  <br>
                  <asp:DropDownList id="SelectionModeList" AutoPostBack="True" OnSelectedIndexChanged="SelectionModeList_SelectedIndexChanged"
                        runat="server">
                        <asp:ListItem Selected="True">Single</asp:ListItem>
                        <asp:ListItem>Multiple</asp:ListItem>
                  </asp:DropDownList>
                  <br>
                  <br>
                  <asp:button id="Button1" Text="Submit" OnClick="Button1_Click" runat="server" />
                  <br>
                  <br>
                  <asp:Label id="Message" runat="server" />
            </form>
      </body>
</HTML>

Then this is the C# CodeBehind page. (Note this is the complete code, most of this is auto generated.) You will need to change the Namespace to whatever you are using.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace <YourNameSpace>
{
      /// <summary>
      /// Summary description for Test.
      /// </summary>
      public class Test : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DropDownList SelectionModeList;
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.ListBox ListBox1;
            protected System.Web.UI.WebControls.Label Message;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.SelectionModeList.SelectedIndexChanged += new System.EventHandler(this.SelectionModeList_SelectedIndexChanged);
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            protected void Button1_Click(Object sender, EventArgs e)
            {

                  Message.Text = "You chose: <br>";
         
                  // Iterate through the Items collection of the ListBox and
                  // display the selected items.
                  foreach (ListItem item in ListBox1.Items)
                  {

                        if(item.Selected)
                        {

                              Message.Text += item.Text + "<br>";

                        }

                  }

            }

            protected void SelectionModeList_SelectedIndexChanged(object sender, System.EventArgs e)
            {

                  // A single-selection ListBox cannot have multiple items selected.
                  // Make sure only one item is selected before changing selection modes.
                  ListBox1.SelectedIndex = 0;

                  // Set the selection mode.
                  ListBox1.SelectionMode = (ListSelectionMode)SelectionModeList.SelectedIndex;
            
            }
      }
}

0
 
LVL 1

Author Comment

by:Artform04
ID: 12171227
Hi,
I simply moved the list box to another page, it works of course. the algorthm I had was valid and correct for some reason if statement wouldnt be entered, I never encounted this error before.

For some strange reason my resx file is missing, is this normal? - do I need to recreate it, things seem to be working fine right now.

ps.: I'll give you the points because you tried to help! cheers.
kind regards,
Martin
0
 
LVL 5

Expert Comment

by:stumpy1
ID: 12171319
I wouldnt worry about the points, you solved the problem yourself so you should probably either just delete the question, or post a question in Community Support Area asking for your points to be refunded and for question to be PAQ'd.

In relation to the .resx file, I deleted it from my example and it still ran for me. Im not sure how you recreate the resx or if you need to. At least its working for you now.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now