I would like to simply deselect this but I am unsure as to where to do it.
Here is there complete source. If I deselect the selected item on the
'SelectedIndexChanged' event then isn't it too late?

Kevin

<Script Runat="Server">

Sub lstProducts_SelectedIndexChanged( s As Object, e As EventArgs )
  lstCart.Items.Add( lstProducts.SelectedItem )
End Sub

Sub btnRemove_Click( s As Object, e As EventArgs )
  lstCart.Items.Remove( lstCart.SelectedItem )
End Sub

Sub btnRemoveAll_Click( s As Object, e As EventArgs )
  lstCart.Items.Clear()
End Sub

</Script>

<html>
<head><title>ListItemCollection.aspx</title></head>
<body>

<form Runat="Server">

<h2>Products:</h2>
<asp:ListBox
  id="lstProducts"
  Width="200"
  AutoPostBack="True"
  OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
  Runat="Server">
  <asp:ListItem Text="Hair Dryer"/>
  <asp:ListItem Text="Shaving Cream"/>
  <asp:ListItem Text="Electric Comb"/>
  <asp:ListItem Text="Nail Polish"/>
  <asp:ListItem Text="French Toast"/>
</asp:ListBox>

<p>

<h2>Shopping Cart:</h2>
<asp:ListBox
  ID="lstCart"
  Width="200"
  Runat="Server"/>
<br>
<asp:Button
  id="btnRemove"
  Text="Remove Item"
  OnClick="btnRemove_Click"
  Runat="Server"/>

<asp:Button
  id="btnRemoveAll"
  Text="Remove All"
  OnClick="btnRemoveAll_Click"
  Runat="Server"/>

</form>
</body>
</html>

-----Original Message-----
From: Patten, Dave [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 29, 2002 6:24 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] ListBox error in ASP.NET

Can also:
                        lbTemp.SelectedIndex = -1;
or
                        lbTemp.SelectedItem.Selected = false;

Before selecting a new Item programmatically.
Shouldn't have to walk the entire Collection.


-----Original Message-----
From: Matt Milner [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 29, 2002 3:51 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] ListBox error in ASP.NET


Either disable view state for the control or walk the collection of items
and set "selected" to false for all but the one you want.  The control is
holding on to the selected setting from the last post and then you are
probably setting another item to be selected.

HTH,
Matt

Matt Milner
BORN
[EMAIL PROTECTED]


-----Original Message-----
From: Kevin Burton
To: [EMAIL PROTECTED]
Sent: 5/29/02 5:05 PM
Subject: [DOTNET] ListBox error in ASP.NET

I have a ListBox that I only want to have a single item selected at a time
and when that item is selected I want it to be copied into another ListBox
(this is an example in ASP.NET Unleashed p. 224 and 225). The first
selection works just fine but the second selection gives me the following
error. It seems that the error is getting thrown before the first selected
item is unselected. Any ideas as to a work around?

Kevin

A ListBox cannot have multiple items selected when the SelectionMode is
Single.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A ListBox cannot have multiple
items selected when the SelectionMode is Single.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): A ListBox cannot have multiple items selected
when the SelectionMode is Single.]
   System.Web.UI.WebControls.ListBox.RenderContents(HtmlTextWriter
writer)
+343
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
+29
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +72
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter
writer)
+44
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
+397
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +72
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
   System.Web.UI.Page.ProcessRequestMain() +1900

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to