473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling Drop Down Selected Item

Hi,

On an ASP.NET page I have a drop down list control. When the user pulls
down the list and makes a selection, I perform validation, and if the
validation fails I want the selected item in the drop down box to go back to
what the value was before the user tried to change it, but at that point I
will not know what the original value was. Or is there a drop down control
"revert" method, or is there any way of knowing what the original selected
item was?

Or, if I can't 'revert', is there a way to keep the drop down list open (if
validation fails) which would force the user to choose again?

(I need to do all of this on the client-side)

Thanks,
John

Dec 21 '05 #1
3 2980
you can keep the original value in a
System.Web.UI.HtmlControls.HtmlInputHidden field and revert back if the
client side validation fails. hope this helps.

"John Walker" <Jo********@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
Hi,

On an ASP.NET page I have a drop down list control. When the user pulls
down the list and makes a selection, I perform validation, and if the
validation fails I want the selected item in the drop down box to go back
to
what the value was before the user tried to change it, but at that point I
will not know what the original value was. Or is there a drop down
control
"revert" method, or is there any way of knowing what the original selected
item was?

Or, if I can't 'revert', is there a way to keep the drop down list open
(if
validation fails) which would force the user to choose again?

(I need to do all of this on the client-side)

Thanks,
John

Dec 21 '05 #2
Hi John,

Given that you want to do this validation and form management
client-side, this topic is proably more suited to a newsgroup about
DHTML scripting, but here goes with the answer anyway...

There are a number of ways you could do this. If it where me, I would
dynamically remove invalid options from the select (drop down) so the
user cannot choose them (this can all be done using javascript quite
easily). However, to achieve the functionality you requested, here's how
I would do it.

First of all, I assume you're delivering the form to the browser in
response to some form of request. When you do, place the original value
of the select into a hidden field. So, let's say the list contains a
bunch of colours for the user to choose from, and the original value is
blue, your form might look something like this:

<form name="frmXyz">
<input type="hidden" name="hdnOriginalColour" value="blue">
<select name="lstColour" onchange="javascript:validateColour();">
<option value="blue" selected>Blue</option>
<option value="red">Red</option>
<option value="green">Green</option>
</select>
</form>

You would of course populate the form in the first place using your
server-side script. This sets up your form. Now in the logic of your
validation function, you're going to do one of two things based on
whether your selection is valid:

1. If it's valid, you update the hidden field to match the new value in
the select, so that an invalid selection at this point returns to the
last valid value.
2. If it's invalid, you change the selectedIndex attribute of the select
to match the text attribute of the selected option to the value in the
hidden field.

My code would look something like this. Bear in mind I haven't tested
this (just wrote it off the top of my head) so you might need to tweak
it a little. Feel free to post again if you have trouble.

<script type="text/javascript">
function validateColour()
{
// Get handles
var objForm = window.document.frmXyz ;
var objList = objForm.lstColour ;
var hdnOrig = objForm.hdnOriginalColour ;

// Is the select option valid?
var strSel = objList.options[objList.selectedIndex].value ;
if( someValidationFunctionOfYourIngeniousDesign(strSel ) ) {

// valid selection, so update the original field so we can revert
later if needed
hdnOrig.value = strSel ;

} else {

// invalid option - revert to last valid option. first, tell the
user how naughty they are
alert('Oops, you did a boo-boo!') ;

// next we need to find the index of the correct option
var intIdx = -1 ;
var intMax = objList.options.length ;
for( var intOpt = 0 ; intOpt < intMax ; intOpt ++ ) {
if( objList.options[intOpt].value == hdnOrig.value ) {
intIdx = intOpt ; // this is the index
intOpt = intMax ; // escape the loop
}
}

// do we have a winner?
if( intIdx == -1 ) {

// something went wrong - we didn't find the original value -
time to panic
throw 'a tantrum' ;

} else {

// set the selected index to the last valid option
objList.selectedIndex = intIdx ;

}
}
</script>

I hope this helps. Good luck!

Cheers,
Mark

John Walker wrote:
Hi,

On an ASP.NET page I have a drop down list control. When the user pulls
down the list and makes a selection, I perform validation, and if the
validation fails I want the selected item in the drop down box to go back to
what the value was before the user tried to change it, but at that point I
will not know what the original value was. Or is there a drop down control
"revert" method, or is there any way of knowing what the original selected
item was?

Or, if I can't 'revert', is there a way to keep the drop down list open (if
validation fails) which would force the user to choose again?

(I need to do all of this on the client-side)

Thanks,
John

Dec 21 '05 #3
Thanks Mark!
This looks like it will do what I need it for. I'll let you know if I can't
get it working.
Thanks!
John

"Mark Micallef" wrote:
Hi John,

Given that you want to do this validation and form management
client-side, this topic is proably more suited to a newsgroup about
DHTML scripting, but here goes with the answer anyway...

There are a number of ways you could do this. If it where me, I would
dynamically remove invalid options from the select (drop down) so the
user cannot choose them (this can all be done using javascript quite
easily). However, to achieve the functionality you requested, here's how
I would do it.

First of all, I assume you're delivering the form to the browser in
response to some form of request. When you do, place the original value
of the select into a hidden field. So, let's say the list contains a
bunch of colours for the user to choose from, and the original value is
blue, your form might look something like this:

<form name="frmXyz">
<input type="hidden" name="hdnOriginalColour" value="blue">
<select name="lstColour" onchange="javascript:validateColour();">
<option value="blue" selected>Blue</option>
<option value="red">Red</option>
<option value="green">Green</option>
</select>
</form>

You would of course populate the form in the first place using your
server-side script. This sets up your form. Now in the logic of your
validation function, you're going to do one of two things based on
whether your selection is valid:

1. If it's valid, you update the hidden field to match the new value in
the select, so that an invalid selection at this point returns to the
last valid value.
2. If it's invalid, you change the selectedIndex attribute of the select
to match the text attribute of the selected option to the value in the
hidden field.

My code would look something like this. Bear in mind I haven't tested
this (just wrote it off the top of my head) so you might need to tweak
it a little. Feel free to post again if you have trouble.

<script type="text/javascript">
function validateColour()
{
// Get handles
var objForm = window.document.frmXyz ;
var objList = objForm.lstColour ;
var hdnOrig = objForm.hdnOriginalColour ;

// Is the select option valid?
var strSel = objList.options[objList.selectedIndex].value ;
if( someValidationFunctionOfYourIngeniousDesign(strSel ) ) {

// valid selection, so update the original field so we can revert
later if needed
hdnOrig.value = strSel ;

} else {

// invalid option - revert to last valid option. first, tell the
user how naughty they are
alert('Oops, you did a boo-boo!') ;

// next we need to find the index of the correct option
var intIdx = -1 ;
var intMax = objList.options.length ;
for( var intOpt = 0 ; intOpt < intMax ; intOpt ++ ) {
if( objList.options[intOpt].value == hdnOrig.value ) {
intIdx = intOpt ; // this is the index
intOpt = intMax ; // escape the loop
}
}

// do we have a winner?
if( intIdx == -1 ) {

// something went wrong - we didn't find the original value -
time to panic
throw 'a tantrum' ;

} else {

// set the selected index to the last valid option
objList.selectedIndex = intIdx ;

}
}
</script>

I hope this helps. Good luck!

Cheers,
Mark

John Walker wrote:
Hi,

On an ASP.NET page I have a drop down list control. When the user pulls
down the list and makes a selection, I perform validation, and if the
validation fails I want the selected item in the drop down box to go back to
what the value was before the user tried to change it, but at that point I
will not know what the original value was. Or is there a drop down control
"revert" method, or is there any way of knowing what the original selected
item was?

Or, if I can't 'revert', is there a way to keep the drop down list open (if
validation fails) which would force the user to choose again?

(I need to do all of this on the client-side)

Thanks,
John

Dec 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2452
by: PT | last post by:
I got a form with many text boxes, checkboxes and 3 drop downs. From that 3, 2 are dependant. I can choose one drop down, and the next drop down should display the dependant values of the first...
6
15394
by: Greg Scharlemann | last post by:
I am attempting to populate a drop down menu based on the selection of a different drop down menu. However, it is not working correctly, I cannot figure out for the life of me what exactly happens...
1
1466
by: Paul M | last post by:
Hi folks, When I set the datasource of a dropdown list and bind it, the drop down list always defaults to the first item in the list. I need it to default to Nothing or NULL or whatever it is...
5
12224
by: Ajith Menon | last post by:
I need to select multiple entries in the drop down list. E.g. Search a string in languages like C#, VB, Java etc. These entries are in drop down. So i need to multi select to search in multiple...
1
2205
by: gubbachchi | last post by:
Hi, How can I get the selected item of the drop down box into a php variable in the same page. The options in drop down box are A,B and C and the code is here <select> <option value="Item...
2
2809
by: phpnewbie26 | last post by:
I currently have two drop down menus where the second one is populated from the first one. My second drop down menu should be able to do multiple selection. I have searched online and found out how...
6
7536
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.