Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > Adobe Web Programming > Dreamweaver (all versions)
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Dreamweaver (all versions) section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old May 14th, 2004, 01:00 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, it's getting clearer. Take a look at this:

if (oList1.selectedIndex == -1){
  oList1.selectedIndex = 0;
 }


At the end of the method, you are explicitly setting the selectedIndex to 0, the first item in the list.

Instead of that, call your function that Snib wrote and pass it the value of the item you want to select. First add the method to your page:

function PreselectMyItem(itemToSelect)
{
  for(myItem in myDropdown)
  {
    if(myItem.text = ValueToSelect) /*if you want to
       // check the value,
       // use myItem.value instead of myItem.text*/
      myItem.selected = true;
  }
}


and then preselect the item in the function you call onload

if (oList1.selectedIndex == -1){
 // oList1.selectedIndex = 0; not needed anymore
  PreselectMyItem('<%=SomeASPVariable%>');
 }

You should find how and what value you pass to SomeASPVariable, the server side ASP variable.

So, now when the page is requested, SomeASPVariable gets a server side value (again, that's up to you to do), and that value is embedded in the call to PreselectMyItem. Then PreselectMyItem makes sure the correct items gets selected.

Does this help? If it doesn't, can you upload a running example of the page somewhere? That is, a page with the real JavaScript and no ASP, so we can test it out a bit?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Kill You by KoRn (Track 14 from the album: Life Is Peachy) What's This?
Reply With Quote
  #12 (permalink)  
Old May 14th, 2004, 02:54 PM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for your reply. At this moment, I don't have any place to upload the example. If you would like, I can attach the file to you.

As for the Snib wrote, I am not very clear about.

var valueToSelect = '<%ASPValueToSelect%>'
function PreselectMyItem(itemToSelect)
{
 for(myItem in myDropdown)
  {
   if(myItem.value = valueToSelect)
   myItem.selected = true;
  }
}

This is how I revised Snib code according to my form.

var valueToSelect = '<%frmTimeOffRequest.mnuName.selectedIndex%>'
function PreselectMyItem(itemToSelect)
{
 for(myItem in myDropdown)
  {
   if(myItem.value = valueToSelect)
   myItem.selected = true;
  }
}

What is myItem, myDropDown. I am getting confused.

Thanks,

Quinn





Reply With Quote
  #13 (permalink)  
Old May 14th, 2004, 03:42 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

OK, a couple things.

1) I forgot to define myItem.

Add this line to the code before the for() loop:
var myItem;

2) Your ASP is messed up:
<%frmTimeOffRequest.mnuName.selectedIndex%>
Does this ASP variable actually exist? It looks like JS.
Anyway you forgot your '=':
<%=frmTimeOffRequest.mnuName.selectedIndex%>

3) myDropDown is your dropdown box.

4) I did not post a function in the first place. I'm not seeing why itemToSelect is in the function you made at all. It's never used.

Let me know if you need more info :)

Snib

<><
Reply With Quote
  #14 (permalink)  
Old May 14th, 2004, 04:14 PM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got the function from Imar suggestion. So what do you mean by this
var valueToSelect = '<%ASPValueToSelect%>'
Why my ASP is messed up? Isn't your code a javascript? What do you mean forgot '=':
Can you be more specific?

Thanks,

Quinn


Reply With Quote
  #15 (permalink)  
Old May 14th, 2004, 04:27 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Sorry, I didn't explain things well....

I think Imar can verify that to write a variable on a page with ASP, one way is to use the shortcut <%=ASPValueToSelect%>. Just using <%ASPValueToSelect%> will not write the variable (is this right Imar? Gosh I need to learn ASP).

ASP and JavaScript are two different things. ASP is server-side, and JavaScript is client-side, although you can use JavaScript as a server-side language through ASP (instead of VBScript). The JavaScript I wrote is meant to be run on the client-side, so is therefore encased in <script> tags. ASP cannot change the page once it is loaded. It pretty much forgets it exists after it sends it.

Let me know if you need more of an explanation.... it took me forever to understand stuff like this :).

Snib

<><
Reply With Quote
  #16 (permalink)  
Old May 15th, 2004, 03:31 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I think some explanation about a few basic principles is in order here.

Consider this:

<script>
  var myClientSide = '<%=myServerSide%>';
</script>

In this code, the <%= %> is a short cut for Response.Write(myServerSide) at the server. So what happens is that a server side variable is written to the client side.
Omitting the = would result in just a variable name listed in the code, but no output.

In your select function, the value of SomeASPVariable I proposed shouldn't be the name of the form element, but the actual value you want to preselect. So, if you have a drop-down with first names, SomeASPVariable could contain John for example, again at the server, to preselect the item wih the value John.

Then this code:

 PreselectMyItem('<%=SomeASPVariable%>');

would end up like this:

 PreselectMyItem('John');

Then the code Snib and I gave you earlier is run. The JavaScript code loops through all the elements in your drop down, looking at its value property, and determines whether it should be selected or not:

I modified the code a bit so it works now, and should run in all browsers:
Code:
<html>
<head>

<script type="text/javascript">
  function PreselectMyItem(itemToSelect)
  {
    // at this point, itemToSelect holds 'John'

    // Get a reference to the drop-down list
    var myDropdownList = document.frmTest.lstNames;

    // Loop through all the items
    for (i = 0; i < myDropdownList.options.length; i++)
    {
      if (myDropdownList.options[i].value == itemToSelect)
      {
        myDropdownList.options[i].selected = true;
        break;
      }
    }
  }
</script>
</head>

<body onload="PreselectMyItem('John');">

<form id="frmTest" name="frmTest">
  <select id="lstNames" name="lstNames">
    <option value="Snib">Snib</option>
    <option value="Imar">Imar</option>
    <option value="John">John</option>
  </select>
</form>
</body>
</html>
The only change to this code you must make is the value you pass to the function. Instead of:

  <body onload="PreselectMyItem('John');">

you should do something like this:

  <body onload="PreselectMyItem('<%=SomeASPVariable%>');">

Instead of passing the item in the onload event, you could also write it out as a variable somewhere in your JavaScript code block.

Does this clarify things??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Kill You by KoRn (Track 14 from the album: Life Is Peachy) What's This?
Reply With Quote
  #17 (permalink)  
Old May 17th, 2004, 01:24 PM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

I tried your suggestion, but the form is not display when the page loaded. Is your drop-down list(lstName) the first down-down list or is it a second drop-down list which depends on the parent drop-down list(department)?

If the value of SomeASPVariable shouldn't be the name of the form element, but I still have to assign it to frmTimeOffRequest.mnuName.value to get the value, is it right? The problem is my name list is populated from the first drop-down list using javascript not using <option> tag.

Thanks,

Quinn


Reply With Quote
  #18 (permalink)  
Old May 17th, 2004, 03:04 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Did you copy the entire example as is? It should work without any modification.

It's not really the first or second listbox. I haven't really tried to create a solution for your problem, as your code is quite dependent on a lot of server side stuff that I don't have here. The example was just meant as a demonstration on how to programmatically preselect an item through JavaScript.

Inspired by this thread, I wrote a FAQ for this: http://Imar.Spaanjaars.Com/QuickDocID.aspx?QUICKDOC=292

It's pretty much the same code as I posted earlier, but with some explanation and a running example you can test out.

Hope this helps a bit.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #19 (permalink)  
Old November 4th, 2004, 03:36 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found the solution!!!!

Dim varMaxLength
varMaxLength = 0
deel1 = 0
While (NOT oDynaListRS.EOF)

 If (varCheckGroup <> oDynaListRS.Fields.Item(txtDynaListRelation).Value ) Then
  If (varCheckLength > varMaxLength) Then
   varMaxLength = varCheckLength
  End If
  varCheckLength = 0
 End If
if (oDynaListRS.Fields.Item(txtDynaListValue).Value) = (rs(txtDynaListValue)) then list1 = deel1
%>
 arrDynaList[<%=(varDynaList+1)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListRelation). Value)%>"
 arrDynaList[<%=(varDynaList+2)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListLabel).Val ue)%>"
 arrDynaList[<%=(varDynaList+3)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListValue).Val ue)%>"
<%
if (oDynaListRS.Fields.Item(txtDynaListRelation).Valu e) = (rs(txtDynaListRelation)) then deel1 = deel1 + 1
 If (len(oDynaListRS.Fields.Item(txtDynaListLabel).Val ue) > len(varMaxWidth)) Then
  varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value
 End If
 varCheckLength = varCheckLength + 1
 varDynaList = varDynaList + 3
 oDynaListRS.MoveNext()
Wend
===========================
rs = the recordset that contains the current product or item or whatever.
Now the variable list1 contains the index of the dropdown that needs to be selected so we can replace:
 oList.selectedIndex = 0;
with
 oList.selectedIndex = <%=list1%>;

Thanx to getting up really early with great coffee and a razorsharp concentration.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
form submit problem/dependent list menus alexscan Pro PHP 0 January 1st, 2008 06:29 PM
Combo Box dependent transferred to another form Ebaad Access VBA 1 May 17th, 2007 12:50 PM
How to retain form values from dependent list ? George B Javascript 0 September 15th, 2006 03:54 PM
Dynamic Dependent List Box improvement SoC Classic ASP Basics 1 August 16th, 2004 01:02 AM
refer to list box on another form timbal25 Access 2 August 5th, 2003 07:05 AM



All times are GMT -4. The time now is 02:48 PM.


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