Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2004, 12:59 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , .
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grab SelectedIndex Number

Hi All,

Is there a way to grab the selectedIndex NUMBER from a select menu and put that number into a variable using ASP(VBScript)? NOTE: I am NOT looking for the VALUE or the TEXT from the select options. Just the INDEX number; just like from an array.

For example, here are my options:

<select size="1" name="D1">
    <option value="test">TEST</option>
    <option value="test2">TEST2</option>
    <option value="test3">TEST3</option>
</select>


If the user select TEST2 from the menu, I want to be able to grab that selectedIndex number(in this case, it would be 1).

Thanks!

 
Old May 14th, 2004, 03:32 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You want to grab it, or you want to grab it and send it to ASP?

Either way, you need JavaScript. Here's an example:


<script language=javascript>

//this function gets the SelectedIndex
function getSelectedIndex(sel)
{
 return sel.selectedIndex;
}

//this function passes the SI to ASP
function passToASP(sel)
{
 var si = getSelectedIndex(sel);
 document.forms.f.SI.value = si;
 document.forms.f.submit();
}
</script>
<form id=f method=post onsubmit=passToASP(this.SI) action=getformvalues.asp>
<input type=hidden id=SI name=SI>
<select size=1 name="D1" id="D1">

</select>
</form>


Then you could refer to the field with ASP as a form element. (something like Request.Form("SI") (I don't know ASP))

HTH,

Snib

<><
 
Old May 14th, 2004, 05:54 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , .
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Thank you for your detailed codes! I am not too familiar with JavaScript. But let me see if I can interpret what you did here. You grab the index number from the select menu, and then put this number into a hidden field. Then, you assign the value of this hidden field into the form, so that you can grab the index number from the form using asp?

Just wanted to make sure what I am doing.

Thanks again.

Leon

 
Old May 14th, 2004, 06:36 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You got it exactly! ;)

HTH,

Snib

<><
 
Old May 17th, 2004, 06:28 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , .
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One last question. What does these codes do?

function getSelectedIndex(sel)
{
 return sel.selectedIndex;
}

Where is "sel" coming from? Aren't you grabbing the selectedindex from the list menu? If so, shouldn't "sel" be "D1" instead, since D1 is the name of your select option.

Thanks again.




Quote:
quote:Originally posted by Snib
 You want to grab it, or you want to grab it and send it to ASP?

Either way, you need JavaScript. Here's an example:


<script language=javascript>

//this function gets the SelectedIndex
function getSelectedIndex(sel)
{
return sel.selectedIndex;
}

//this function passes the SI to ASP
function passToASP(sel)
{
var si = getSelectedIndex(sel);
document.forms.f.SI.value = si;
document.forms.f.submit();
}
</script>
<form id=f method=post onsubmit=passToASP(this.SI) action=getformvalues.asp>
<input type=hidden id=SI name=SI>
<select size=1 name="D1" id="D1">

</select>
</form>


Then you could refer to the field with ASP as a form element. (something like Request.Form("SI") (I don't know ASP))

HTH,

Snib

<><
 
Old May 17th, 2004, 06:54 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

sel is a temporary variable local to the function getSelectedIndex(). The way I constructed this code, you can use it with any form just the way I put it here. If you directly referred to the form, you would have to change the function for every form you wanted. This way, the form object is passed to the function (whichever form you want) every time you call it, and it uses that handle as a kind of shortcut to the form.

Hope I've helped you understand it better.

Snib

<><
 
Old May 18th, 2004, 10:14 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , .
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all your help Snib! Your codes make sense now. :-)

Leon





Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndex in datalist dhoward VB.NET 2002/2003 Basics 0 August 28th, 2008 10:15 AM
how to set selectedindex azitanosrati General .NET 16 December 1st, 2006 02:25 PM
GridView.SelectedIndex Prop SteveP55419 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 8th, 2006 09:08 PM
how can get selectedindex in dropdown list azitanosrati General .NET 0 September 8th, 2004 05:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.