Solved

How to validate user selection of a ListBox

Posted on 2008-10-20
14
691 Views
Last Modified: 2012-05-05
Greetings:

I have a list box on an ASP page with a selection range from 0 - 100.  I've been validating the user input like this:

        if (ListBox1.SelectedIndex != 0)
        {
            SAVE THE USERS SELECTION
            ...
        }

However, I'm finding that if the user selects (high-lights) an item (say index 1) and then scrolls down the list to item 10, but doesn't select (highlight) item 10 then item 1 ends up being the value processed.

In the picture below, the first item of the ListBox is truly selected because it is high-lighted, but it's possible to use the scroll bar/arrows to display another value and the user thinks this value is selected just because it is displayed.

Is there another property associated with ListBoxes that would enable me to test something like this:

if ((ListBox1.SelectedIndex != 0) && (ListBox1 IS CURRENTLY HIGH-LIGHTED))
then ....

Thanks!!

group-box.JPG
0
Comment
Question by:John500
  • 7
  • 6
14 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 22761701
I used this and it worked
<asp:ListBox ID="lbList" runat="server" Height="50px">

    <asp:ListItem Text="1" Value="1"></asp:ListItem>

    <asp:ListItem Text="2" Value="2"></asp:ListItem>

    <asp:ListItem Text="3" Value="3"></asp:ListItem>

    <asp:ListItem Text="4" Value="4"></asp:ListItem>

    <asp:ListItem Text="5" Value="5"></asp:ListItem>

    <asp:ListItem Text="6" Value="6"></asp:ListItem>

    <asp:ListItem Text="7" Value="7"></asp:ListItem>

    <asp:ListItem Text="8" Value="8"></asp:ListItem>

    <asp:ListItem Text="9" Value="9"></asp:ListItem>

    <asp:ListItem Text="10" Value="10"></asp:ListItem>

    <asp:ListItem Text="11" Value="11"></asp:ListItem>

    <asp:ListItem Text="12" Value="12"></asp:ListItem>

    <asp:ListItem Text="13" Value="13"></asp:ListItem>

    <asp:ListItem Text="14" Value="14"></asp:ListItem>

    <asp:ListItem Text="15" Value="15"></asp:ListItem>

    <asp:ListItem Text="16" Value="16"></asp:ListItem>

    <asp:ListItem Text="17" Value="17"></asp:ListItem>

    <asp:ListItem Text="18" Value="18"></asp:ListItem>

    <asp:ListItem Text="19" Value="19"></asp:ListItem>

</asp:ListBox>
 

<asp:Button ID="cmdVerify" runat="server" Text="Verify" OnClick="cmdVerify_Click" />
 

--C#
 

protected void cmdVerify_Click(object sender, EventArgs e)

{

    Response.Output.Write(lbList.SelectedItem.Text);

}

Open in new window

0
 

Author Comment

by:John500
ID: 22761802
Thanks, but can you explain it?  Although I mentioned a range from 0 to 100 it could vary.  Are you saying I would need to list all of these out like:

<asp:ListBox ID="lbList" runat="server" Height="50px">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    ....
   <asp:ListItem Text="100" Value="100"></asp:ListItem>

If so, this won't work just because this ListBox is too dynamic.  Otherwise, if listing all the values is not necessary, I don't understand the C# portion of the code?
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22761885
Hi John500,
If you allow multiple selection, then don't set ListBox1.Rows = 1. It must be greater than 1 to avoid the problem you are having.
0
 

Author Comment

by:John500
ID: 22761925
... this routine only allows one selection

Is there another property that relates to high-lighting?
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22762004
Well, it seems to me by looking at the screen shot, you set SelectionMode="Multiple", because otherwise, it will only be one triagle pointing down, not two. When ListBox's SelectionMode="Single", it will become DropDownList.
2008-10-20-154704.jpg
0
 

Author Comment

by:John500
ID: 22762092
I wish that were the case because it would be the perfect way to make it obvious to the user.  Any idea why the property indicates single yet it appears multiple?

Maybe I should just set it during run-time?


listbox-properties.JPG
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22762173
Maybe somewhere in the code-behind, the SelectionMode is set to Multiple?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:John500
ID: 22762210
There's nothing in the code that makes this assignment but I'd like to test it in single mode.  What would the syntax be??

I've tried the following:

       ListBox1.SelectionMode = Single;    // error on the word 'Single'

But I get this error:

'float' is a 'type' but is used like a 'variable'

What's missing here?

Thanks
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22762247

ListBox1.SelectionMode = ListSelectionMode.Single;

Open in new window

0
 

Author Comment

by:John500
ID: 22762392
Thanks.  This code compiled and I placed it in the page render section like so:

 protected void Page_PreRender(object sender, EventArgs e)
    {
        int type = 0;
        string code = Code;
        ListBox1.SelectionMode = ListSelectionMode.Single;
        ...
}

But this still doesn't produce a single drop-down arrow.  Is it possible the property has something to do with 'drop-down' or something else like that??

Thanks
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22762518
No such property, as far as I know.
1. Try removing ListBox's Height value.
2. Try doing a search for "ListSelectionMode" in Current Project to see if you find anything.
If all fail, then you may want to use DropDownList, which is very suitable for your situation.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22762537
Wait a second. What is your ListBox's Rows? It needs to be 1 to make a drop-down list arrow. Refer my comment 22762004.
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22762561
ListBox1.SelectionMode="Single" and ListBox1.Rows = 1 ====>DropDownList look and feel.
0
 

Author Closing Comment

by:John500
ID: 31507989
That was it - I had 4 rows.  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tool Box 2 34
Facial recognition to look through the whole database for a person 3 39
IIS Authorization for Web Service 2 21
C# parent child form 5 14
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

17 Experts available now in Live!

Get 1:1 Help Now