Solved

Multi Select LIST-BOX problem

Posted on 2004-09-28
6
335 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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