p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Dreamweaver (all versions) (http://p2p.wrox.com/forumdisplay.php?f=58)
-   -   Retain form value from dependent list box (http://p2p.wrox.com/showthread.php?t=12922)

Imar May 14th, 2004 01:00 PM

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?

qhong May 14th, 2004 02:54 PM

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






Snib May 14th, 2004 03:42 PM

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

<><

qhong May 14th, 2004 04:14 PM

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



Snib May 14th, 2004 04:27 PM

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

<><

Imar May 15th, 2004 03:31 AM

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?

qhong May 17th, 2004 01:24 PM

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



Imar May 17th, 2004 03:04 PM

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.

deeoo November 4th, 2004 03:36 AM

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.



All times are GMT -4. The time now is 03:47 AM.

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