4 Replies - 638 Views - Last Post: 12 May 2012 - 09:51 AM Rate Topic: -----

#1 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Adding item only once in dropdownlist

Posted 12 May 2012 - 02:55 AM

Hello, I am using ASP.NET with VB.NET as background code. I have 8 ImageMaps in my webform Estimator.aspx.
Each ImageMap represents a car part (Front Bumper, Rear Bumper, Mirror, Hood, Sunroof, DoorShell, Headlights, Tail lights) and I have a dropdownlist ddlChosenParts. When the user clicks on any of the 8 ImageMaps, I add the PostBackValue to the dropdownlist ddlChosenParts.

The problem is that if the user clicks on the same ImageMap twice, the PostBackValue is added twice.

I want to check if ddlChosenParts contains the ImageMap's PostBackValue and if it already contains it, do not add it again. That means that if the user clicks more than once on the same ImageMap, it's PostBackValue is only added once in ddlChosenParts.

I have this code but i'm getting an error: Value of Type String cannot be converted to ListItem. And I know why i'm getting this error but just can't seem to solve it.

     Protected Sub imFrontBumper_Click(sender As Object, e As System.Web.UI.WebControls.ImageMapEventArgs) Handles imFrontBumper.Click
        If ddlChosenParts.Items.Contains(e.PostBackValue) Then
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = " - Part '" & e.PostBackValue & "' already added"
        Else
            ddlChosenParts.Items.Add(e.PostBackValue)
            lblStatus.ForeColor = Drawing.Color.Blue
            lblStatus.Text = " - Part '" & e.PostBackValue & "' added to list of damaged parts"
        End If
    End Sub



This code is the same for the 8 ImageMaps.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding item only once in dropdownlist

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Adding item only once in dropdownlist

Posted 12 May 2012 - 05:05 AM

have you tried e.ParseBackValue.ToString?
Was This Post Helpful? 0
  • +
  • -

#3 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Adding item only once in dropdownlist

Posted 12 May 2012 - 05:15 AM

Yes CharlieMay, I tried .toString but i'm getting the same error: Cannot convert from String to ListItem. And by ParseBackValue you mean PostBackValue right?
Was This Post Helpful? 0
  • +
  • -

#4 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 74
  • Joined: 14-April 12

Re: Adding item only once in dropdownlist

Posted 12 May 2012 - 07:48 AM

ddlChosenParts.Items.Contains takes only list item, try like this:

If ddlChosenParts.Items.Contains(New ListItem(e.PostBackValue)) Then

Was This Post Helpful? 1
  • +
  • -

#5 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Adding item only once in dropdownlist

Posted 12 May 2012 - 09:51 AM

Thank you zeeshanef, it worked! I knew that the ddlChosenParts.items.contains only takes ListItem but I didn't know that new works on ListItem.

Problem solved :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1