Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Multi Select LIST-BOX problem

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
Artform04
Asked:
Artform04
  • 3
  • 3
1 Solution
 
Artform04Author Commented:
Im sure somebody must know the solution or at least had a simillar problem with a mobile:selectionList (listbox).
0
 
Artform04Author Commented:
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
 
stumpy1Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
stumpy1Commented:
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
 
Artform04Author Commented:
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
 
stumpy1Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now