Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate dropdown lists accurately 1

Status
Not open for further replies.

pnad

Technical User
May 2, 2006
133
0
0
US
Hello,

I have a dropdown list box on an ASP page that is supposed to have three items: None, Approved, Disapproved.
So the user needs to select one of the above and saves the form. Then when he visits the page, his selection should reflect on the page again. I am using the following code but it doesnt seem to work:

function SetCboSelected(OptionsTxt, SelectedValue)
Dim sFind, pos, LineStart, re

if len(SelectedValue) = 0 then
SetCboSelected = OptionsTxt
exit function
end if
set re = new RegExp

re.IgnoreCase = true
re.Multiline = true
re.Global = false

re.Pattern = "(<option value=)([\""']?" & SelectedValue & "[\""'])"

SetCboSelected = re.Replace(OptionsTxt, "$1$2 Selected class=""optSelected""")
end function

<th><select id="ApprovalIndicator" style="width:220px" name="ApprovalIndicator" onchange="document.body.focus();">

<%dim optApprovalIndicator

optApprovalIndicator = "<option value=""B"">&nbsp;</option>"
optApprovalIndicator = optApprovalIndicator & "<option value=""A"">Approved</option>"
optApprovalIndicator = optApprovalIndicator & "<option value=""D"">Disapproved</option>"
Response.Write(SetCboSelected(optApprovalIndicator,RS1("APPROVALINDICATOR")))
%>

</select></th>

So basically, I am using regular expressions to match what the user selected and saved in the DB so thats what the form displays.

Thanks.
 
I'm not quite sure why you are using RegEx for this (I'm probably missing something), but this is how I would do it:
Code:
<html>
	<head>
		<script>
			function page_init()
			{
				var o_field = document.getElementById("ApprovalIndicator");
				for (var i=0; i<o_field.length; i++)
				{
					if (o_field.options[i].value == "<%= rs1("APPROVALINDICATOR") %>")
					{
						o_field.options[i].selected = true;
					}
				}
			}
		</script>
	</head>
	<body onload="page_init();">
		<table>
			<th>
				<select id="ApprovalIndicator" style="width:220px" name="ApprovalIndicator" onchange="document.body.focus();">
					<option value="B">&nbsp;</option>
					<option value="A">Approved</option>
					<option value="D">Disapproved</option>
				</select>
			</th>
		</table>
	</body>
</html>

Take Care,
Mike
 
Thanks ! It worked like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top