Print Page | Close Window

Check variable before applying?

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=20281
Printed Date: 28 March 2026 at 9:37am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Check variable before applying?
Posted By: davidshq
Subject: Check variable before applying?
Date Posted: 12 June 2006 at 3:32am
I have a variable which is applied to a dropdownlist, to set which item in the dropdownlist is by default selected. The problem is, that based upon another dropdownlist the items available in the second dropdownlist are determined. Therefore, while ID 10 might be valid if one selects option one from the first dropdownlist, it might not be if option two. For example, if one selects Pennsylvania ID 5 (linked to bucks county) may be valid, but if they select New York ID 5 is not going to be valid b/c NY does not have a Bucks County.
Now the problem is that it is conceivable that the variable will contain an ID which is not in the dropdownlist, so i need to check the variable before I apply it. In pseudo code something like this:
If varA is in DropDownList.Values Then
DropDownList.SelectedValue = varA
End If
That way I don't crash every time the variable isn't what it should be. Any thoughts?
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Replies:
Posted By: michael
Date Posted: 12 June 2006 at 2:17pm
Why don't you just fill the counties listbox when the state listbox selectedvalue has been changed.

You can also use Atlas (atlas.asp.net) to do this beautifully without a postback.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: davidshq
Date Posted: 14 June 2006 at 4:29am
I actually already have that implemented. When the state is selected it postsback and selects the counties in that state. Here is an example of how the problem arises:
Jane visits BetterNeighbours. She currently lives in Albany County New York. She sets Albany County as her favorite county. She is moving to Bucks County Pennsylvania. She has moved some stuff to Bucks and then realizes she doesn't need it, so she decides to list it on better neighbours. She clicks on Pennsylvania for the state and Better Neighbours creates a list of counties in a dropdownlist that are in PA. Then Better Neighbours attempts to set the focus of the DropDownList to the favorite county, but because the favorite county is in New York it isn't party of the dropdownlist and the program crashes.
   I think I have figured out a way to do it however. I can say:
If SelectedCounty.State != SelectedStateID Then
   DropDownList1.Default
Else
   DropDownList1.SelectedValue = SelectedCounty.State
(pseudo-code)
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net