472,807 Members | 3,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

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 2919
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.