can't use "=" sign in asp.net

cfisher440

Well-known member
Joined
Oct 11, 2005
Messages
73
Programming Experience
1-3
I am basically converting a VB .NET app to an ASP .NET app
I have three listboxes. Selecting one item from on list box, will populate another listbox with items.
Example - listbox 1 has values Car and boat. If you select car, the next list box has items "Ford" and "Honda"

In VB.net the code looks something like this

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]'Put user code to initialize the page here[/COLOR][/SIZE]
[SIZE=2]box1.Items.Add("Car")[/SIZE]
[SIZE=2]box1.Items.Add("Boat")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] box1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] box1.SelectedIndexChanged[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] box1.SelectedItem = "Car" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box2.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]box2.Items.Clear()[/SIZE]
[SIZE=2]box2.Items.Add("Ford")[/SIZE]
[SIZE=2]box2.Items.Add("Honda")[/SIZE]
[SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] box1.SelectedValue Is [/SIZE][SIZE=2]"Boat" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box2.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]box2.Items.Clear()[/SIZE]
[SIZE=2]box2.Items.Add("Small")[/SIZE]
[SIZE=2]box2.Items.Add("Big")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

In asp.net when I do this I get a blue squiggly line under
"if box1.SelectedItem = "Car" then"
It says Operator '=' is not defined for types 'System.Web.UI.Webcontrols.ListItem' and 'String'. Use Is operator to compare two reference types.
I used the 'Is' operator but it's not making the next list box visible

when I use if box1.SelectedIndex = 0 then
I do not recieve a blue squiggly line. But when I run the app and select Car nothing happens. Nothing shows up in box2

any questions / comments / ideas / solutions would be appreciated
 
Last edited:
The documentation tells you that SelectedItem is a ListItem. ListItem got several properties useful, for instance Text and Value. But you can't compare ListItem and String with = operator, that is correct. In your elseif you do something different, you use SelectedValue which property is type String, and you compare this string by reference IS with another String. I'm inclined to ask why you do both these two different comparisons backwards is regards to their data types, but I won't. So compare your two strings with = operator, be it SelectedValue or SelectedItem.Text or whatever.
 
Thank you for the feedback.

I just did it that way to show you the way I was doing it compared to what VB.NET was telling me to do.
If box1.SelectedItem = "Car" Then is what I am using in VB .NET and it is working, but not with ASP .NET
box1.SelectedValue Is "Boat" as opposed to box1.SelectedValue = "Boat" is what ASP .NET is telling me I have to do.

Here is what I want to do. There is two list boxes. One is visible and one is not. One that is visible has the items car and boat. If you select car then the other listbox becomes visible and displays the items Ford and Honda. Else if you select boat, the listbox displays items Small and Large.

Able to do it in VB .NET but when I do the same thing as an ASP .NET app it doesn't work. gives me the blue squiggly line

I will try messing with the values your talking about with listbox control, because I know there is a way to do this.
 
Page must have to be refreshed?

I added a button and a label to the page. When you press the button it shows the SelectedValue in the label. Well I decided to leave the rest of the code in as well and it populated the other listbox with the car values Ford and Honda after the button was pressed. I'm thinking the reason it was not popping up was not because my code was wrong, but because since it is web page. the page must be refreshed each time an item is selected.
 
compare string with

your code

If box1.SelectedItem = "Car" Then

actually compares an object with a string. you need to compare the text property to your string like so:

If box1.SelectedItem.Text = "Car" Then

The windows dropdownbox control uses the .Text property as a default, not so the ASP control.

I hope this helps.
G.
 
Thank you for that clarification. There is still a problem though. When I select Car from box1, it should automatically populate box2 with Ford and Honda, without the page having to be refreshed, but it does not.
So I put a button on the page and when you hit it, it puts the text of the selecteditem from box1 in the label. Here is the great part, it makes box2 visible with the values Ford and Honda (which is what I want). But it also add another instance of Car and Boat to the first listbox (so now there are to Car and two Boat values (don't want that)).

I am thinking it has something to do with it being server side code as opposed to client side scripting, because I have seen this done before using javascript and the page doesn't have to refresh in order for this to happen.
Is there a way to make the page refresh after selecting an item without having to press a button?

Here is the new code from the suggestions u all have given me

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]'Put user code to initialize the page here[/COLOR][/SIZE]
[SIZE=2]box1.Items.Add("Car")[/SIZE]
[SIZE=2]box1.Items.Add("Boat")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] box1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] box1.SelectedIndexChanged[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] box1.SelectedItem.Text = "Car" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box2.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]box2.Items.Clear()[/SIZE]
[SIZE=2]box2.Items.Add("Ford")[/SIZE]
[SIZE=2]box2.Items.Add("Honda")[/SIZE]
[SIZE=2]lblOutput.Text = "skrew that, go for a Hummer!"[/SIZE]
[SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] box1.SelectedItem.Text = "Boat" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box2.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]box2.Items.Clear()[/SIZE]
[SIZE=2]box2.Items.Add("Small")[/SIZE]
[SIZE=2]box2.Items.Add("Big")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#000000] btnSubmit_Click([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Object, [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.EventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#000000] btnSubmit.Click[/COLOR][/SIZE]
[SIZE=2]lblOutput.Text = box1.SelectedItem.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
Remember, everything happens on the server. So when you click a button in the browser, it posts back to the server and your page is build again and the new page is send to the browser.
So after clicking your button, you run through the Page_Load event again and there you populate the box1 a second time. So you need to check if this is he first time a page is build or if this is a postback. This is done like so:

PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
'Put user code to initialize the page here
If Not IsPostBack then
'this part is only executed the first time a page loads.
box1.Items.Add("Car")
box1.Items.Add("Boat")
end if
EndSub

As for your box1.selectedindexchanged event, it should fire when you change the selection in your box1. Why don't you set a breakpoint there and see if this event fires.

Have fun
G.
 
Thank you for the first part, that is working out well.

I set a breakpoint on the box1_selectedindexchanged, but nothing happens.
Is there a way to make the items selected in a listbox fire events?
This works in a VB .NET app but not in as ASP .NET app

I just made a simple little program with a label and a listbox.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]'Put user code to initialize the page here[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsPostBack [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box1.Items.Add("Car")[/SIZE]
[SIZE=2]box1.Items.Add("Boat")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] box1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] box1.SelectedIndexChanged[/SIZE]
[SIZE=2]lblOutput.Text = box1.SelectedItem.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
NOthing happens here

Same one but with a button added, and the
lblOutput.Text = box1.SelectedItem.Text put into the button's event
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]'Put user code to initialize the page here[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsPostBack [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]box1.Items.Add("Car")[/SIZE]
[SIZE=2]box1.Items.Add("Boat")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2]lblOutput.Text = box1.SelectedItem.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
This does work. The selectedItem goes to the label.
Now, just to get it to go without having to insert a button into the program?
 
Take a look at the property "AutoPostBack" of the dropdownlistbox control. It has to be set to "TRUE". If this is not set, the selectedindex event will not fire.

Lot's of little things to learn, I know. But it feels great when you get it working.

Have fun and just let us know if your issues are solved.

G.
 
Back
Top