案例:
数据库里有两个表,一个是产品表,一个是类别表,有个asp页面要实现修改数据的功能,该页面中有个动态列表框,直接绑定到类别表上,值是ID,标签是类别名称
问题是:
当把产品表的数据读取到表单的时候,怎样能使列表框的选项直接显示产品的类别呢?
案例:
数据库里有两个表,一个是产品表,一个是类别表,有个asp页面要实现修改数据的功能,该页面中有个动态列表框,直接绑定到类别表上,值是ID,标签是类别名称
问题是:
当把产品表的数据读取到表单的时候,怎样能使列表框的选项直接显示产品的类别呢?
[CODE]<script language="JavaScript">
var TAry=new Array(2);
TAry[0]="东城|西城|宣武|朝阳|海淀|丰台|门头沟|大兴";
TAry[1]="黄浦|卢湾|徐汇|长宁|静安|普陀|闸北|虹口|杨浦|闵行|宝山|嘉定|浦东|金山|松江|青浦|南汇|奉贤|崇明";
function SelCity(val)
{
var SAry=TAry[val].split("|");//将有规律的字符串变成数组
this.SelTown.options.length=SAry.length;//(关键)列表控件的选项长度可以改变
//循环将数组的内容加入到列表控件内
for(var i=0;i<SAry.length;i++)
{
this.SelTown.options[i].value=SAry[i];
this.SelTown.options[i].text=SAry[i];
}
}
</script>
<select name="SelCity" onChange="SelCity(this.options[this.selectedIndex].value);" >
<option value="0" >北京</option>
<option value="1" >上海</option>
</select>
<select name="SelTown" >
<option selected >请选择城镇</option>
</select>[/CODE]
[此贴子已经被作者于2006-12-12 21:15:46编辑过]
楼主看看下面的能不能派上用处。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="../Connections/Coon.asp" -->
<%
Dim Rs
Dim Rs_numRows
Set Rs = Server.CreateObject("ADODB.Recordset")
Rs.ActiveConnection = MM_Coon_STRING
Rs.Source = "SELECT * FROM ClassBig ORDER BY M_id ASC"
Rs.CursorType = 0
Rs.CursorLocation = 2
Rs.LockType = 1
Rs.Open()
Rs_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index
Repeat1__numRows = -1
Repeat1__index = 0
Rs_numRows = Rs_numRows + Repeat1__numRows
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="2">
<tr>
<td height="25" colspan="2" background="../Images/bg1.gif"> 后台管理 >> 类别管理</td>
</tr>
<tr>
<td width="442">已有大类别:</td>
<td width="300">对应小类别</td>
</tr>
<tr>
<td valign="top"><table width="100%" border="1" cellpadding="5" cellspacing="0" bordercolor="#D2F6CF">
<tr>
<td align="center" bgcolor="#EDF7E7">名称</td>
<td align="center" bgcolor="#EDF7E7">序号</td>
<td align="center" bgcolor="#EDF7E7">管理</td>
</tr>
<%
While ((Repeat1__numRows <> 0) AND (NOT Rs.EOF))
%>
<tr>
<td align="center"><%=(Rs.Fields.Item("M_Name").Value)%></td>
<td align="center"><%=(Rs.Fields.Item("M_Order").Value)%></td>
<td align="center"><a href="TypeAdmin.asp?action=modify&id=<%=(Rs.Fields.Item("M_id").Value)%>">修改</a> | <a href="typeadmin.asp?del=yes&id=<%=(Rs.Fields.Item("M_id").Value)%>">删除</a></td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Rs.MoveNext()
Wend
%>
</table>
<%
Rs.Close()
Set Rs = Nothing
%></td>
<td><IFRAME ID="Sm" src="TypeSmallView.asp?id=1" frameborder="0" scrolling="no" width="300" height="200"></IFRAME></td>
</tr>
<tr>
<td valign="top">
<%if request("action")="modify" then
id=request.querystring("id")
set rs=server.createobject("adodb.recordset")
sql1="select * from ClassBig where M_id= "&id
rs.open sql1,MM_Coon_STRING,1,1
%>
修改现有大类别:<br />
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form id="form1" name="form1" method="post" action="TypeAdmin.asp?go=yes&id=<%=rs("M_id")%>">
<tr>
<td>新名称</td>
<td>
<label for="textfield"></label>
<input name="TT" type="text" id="TT" value="<%=rs("M_Name")%>" />
<input type="submit" name="Submit" value="提交" id="Submit" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</form>
</table>
<%rs.close
set rs=Nothing
%>
<%end if %> </td>
<td>
<% if request("del")="yes" then
id=request.querystring("id")
set rs=server.createobject("adodb.recordset")
sql="select * from ClassBig where M_id="&id
rs.open sql,MM_Coon_STRING,1,3
rs.delete
rs.update
set rs=nothing
Response.redirect(request.servervariables("http_referer"))
end if
%>
<table width="100%" border="1" cellpadding="5" cellspacing="0" bordercolor="#D2F6CF">
<form id="form2" name="form2" method="post" action="typeadmin.asp?add=big">
<tr>
<td colspan="3" bgcolor="#EDF7E7">新增
大类别:</td>
</tr>
<tr>
<td align="center">名称</td>
<td align="center">序号</td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"><input name="BName" type="text" id="BName" size="10" /></td>
<td align="center"><input name="BCode" type="text" id="BCode" size="10" /></td>
<td align="center"> </td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit22" value="提交" id="Submit2" /></td>
</tr> </form>
<tr>
<td colspan="3" bgcolor="#EDF7E7">新增小类别:
</td>
</tr>
<form id="form3" name="form3" method="post" action="typeadmin.asp?add=small">
<tr>
<td align="center">名称</td>
<td align="center">对应大类别序号</td>
<td align="center">小类别序号</td>
</tr>
<tr>
<td align="center"><label for="textfield"></label>
<input name="Snam" type="text" id="Snam" size="10" /></td>
<td align="center"><input name="SBcode" type="text" id="SBcode" size="10" /></td>
<td align="center"><input name="Scode" type="text" id="Scode" size="10" /></td>
</tr>
<tr>
<td colspan="3" align="center"><label for="label"></label>
<input type="submit" name="Submit2" value="提交" id="label" /></td>
</tr> </form>
</table></td>
</tr>
</table>
<%if request("add")="big" then
BName=request.form("BName")
BCode=request.form("BCode")
if BName="" or BCode="" then
response.write "<SCRIPT language=JavaScript>alert('Please write the txt , is not right now!');"
Response.Write"this.location.href='vbscript:history.back()';</SCRIPT>"
Response.End
end if
'检验类型名称,类型名称不能重复
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from ClassBig where M_Name='"&LCase(trim(request.form("BName")))&"'" ,MM_Coon_STRING,1,1
if rs.recordcount>0 then
response.write "<script language=javascript>"
response.write "alert('Sorry, this type of the name is exist in the database now!');"
response.write "this.location.href='vbscript:history.back()';</script>"
Response.end
else
'检验类型编号,类型编号不能重复。
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from ClassBig where M_Order="&BCode ,MM_Coon_STRING,1,1
if rs.recordcount>0 then
response.write "<script language=javascript>"
response.write "alert('Sorry, this Order of the id is exist in the database now!');"
response.write "this.location.href='vbscript:history.back()';</script>"
Response.end
else
set rs=server.createobject("adodb.recordset")
sql="select * from ClassBig "
rs.open sql,MM_Coon_STRING,1,3
rs.addnew
rs("M_Name")=BName
rs("M_Order")=BCode
rs.update
rs.close
set rs=nothing
Response.redirect(request.servervariables("http_referer"))
end if
end if
end if
%>
<%
if request("add")="small" then
BName=request.form("Snam")
BCode=request.form("SBcode")
SCode=request.form("SCode")
if BName="" or BCode="" then
response.write "<SCRIPT language=JavaScript>alert('Please write the txt , is not right now!');"
Response.Write"this.location.href='vbscript:history.back()';</SCRIPT>"
Response.End
end if
'检验类型名称,类型名称不能重复
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from ClassSm where M_Title='"&LCase(trim(request.form("BName")))&"'" ,MM_Coon_STRING,1,1
if rs.recordcount>0 then
response.write "<script language=javascript>"
response.write "alert('Sorry, this type of the name is exist in the database now!');"
response.write "this.location.href='vbscript:history.back()';</script>"
Response.end
else
'检验类型编号,类型编号不能重复。
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from ClassSm where M_Class="&BCode ,MM_Coon_STRING,1,1
if rs.recordcount>0 then
response.write "<script language=javascript>"
response.write "alert('Sorry, this Order of the id is exist in the database now!');"
response.write "this.location.href='vbscript:history.back()';</script>"
Response.end
else
set rs=server.createobject("adodb.recordset")
sql="select * from ClassSm "
rs.open sql,MM_Coon_STRING,1,3
rs.addnew
rs("M_Title")=BName
rs("M_Big")=SBcode
rs("M_Class")=BCode
rs.update
rs.close
set rs=nothing
Response.redirect(request.servervariables("http_referer"))
end if
end if
end if
%>
</body>
</html>
分类的:
都是刚刚这两天我些程序的,顺便也给你看看。
<%
Dim Pr
Dim Pr_numRows
Set Pr = Server.CreateObject("ADODB.Recordset")
Pr.ActiveConnection = MM_Coon_STRING
Pr.Source = "SELECT * FROM ClassBig ORDER BY M_id ASC"
Pr.CursorType = 0
Pr.CursorLocation = 2
Pr.LockType = 1
Pr.Open()
Pr_numRows = 0
%>
<%
Dim Cr
Dim Cr_numRows
Set Cr = Server.CreateObject("ADODB.Recordset")
Cr.ActiveConnection = MM_Coon_STRING
Cr.Source = "SELECT * FROM ClassSm ORDER BY M_id ASC"
Cr.CursorType = 0
Cr.CursorLocation = 2
Cr.LockType = 1
Cr.Open()
Cr_numRows = 0
%>
<script language="JavaScript">
<!--
var arrDynaList = new Array();
var arrDL1 = new Array();
arrDL1[1] = "BigOne"; // Name of parent list box
arrDL1[2] = "form1"; // Name of form containing parent list box
arrDL1[3] = "SmailOne"; // Name of child list box
arrDL1[4] = "form1"; // Name of form containing child list box
arrDL1[5] = arrDynaList;
<%
Dim txtDynaListRelation, txtDynaListLabel, txtDynaListValue, oDynaListRS
txtDynaListRelation = "M_Big" '// Name of recordset field relating to parent
txtDynaListLabel = "M_Title" ' // Name of recordset field for child Item Label
txtDynaListValue = "M_Class" '// Name of recordset field for child Value
Set oDynaListRS = Cr '// Name of child list box recordset
Dim varDynaList
varDynaList = -1
Dim varMaxWidth
varMaxWidth = "1"
Dim varCheckGroup
varCheckGroup = oDynaListRS.Fields.Item(txtDynaListRelation).Value
Dim varCheckLength
varCheckLength = 0
Dim varMaxLength
varMaxLength = 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
%>
arrDynaList[<%=(varDynaList+1)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListRelation).Value)%>"
arrDynaList[<%=(varDynaList+2)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListLabel).Value)%>"
arrDynaList[<%=(varDynaList+3)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListValue).Value)%>"
<%
If (len(oDynaListRS.Fields.Item(txtDynaListLabel).Value) > len(varMaxWidth)) Then
varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value
End If
varCheckLength = varCheckLength + 1
varDynaList = varDynaList + 3
oDynaListRS.MoveNext()
Wend
If (varCheckLength > varMaxLength) Then
varMaxLength = varCheckLength
End If
%>
//-->
</script>
<script language="JavaScript">
<!--
function setDynaList(arrDL){
var oList1 = document.forms[arrDL[2]].elements[arrDL[1]];
var oList2 = document.forms[arrDL[4]].elements[arrDL[3]];
var arrList = arrDL[5];
clearDynaList(oList2);
if (oList1.selectedIndex == -1){
oList1.selectedIndex = 0;
}
populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
return true;
}
function clearDynaList(oList){
for (var i = oList.options.length; i >= 0; i--){
oList.options[i] = null;
}
oList.selectedIndex = -1;
}
function populateDynaList(oList, nIndex, aArray){
for (var i = 0; i < aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
}
}
if (oList.options.length == 0){
oList.options[oList.options.length] = new Option("[none available]",0);
}
oList.selectedIndex = 0;
}
function Kermy_JS(jsStr) {
return eval(jsStr)
}
//-->
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="222">
<tr>
<td colspan="2">请选择您的文章类型:</td>
</tr>
<tr>
<td width="102">大类型:</td>
<td width="648"><label for="select"></label>
<select onChange="Kermy_JS('setDynaList(arrDL1)')" name="BigOne" id="BigOne">
<%
While (NOT Pr.EOF)
%>
<option value="<%=(Pr.Fields.Item("M_Order").Value)%>"><%=(Pr.Fields.Item("M_Name").Value)%></option>
<%
Pr.MoveNext()
Wend
If (Pr.CursorType > 0) Then
Pr.MoveFirst
Else
Pr.Requery
End If
%>
</select> </td>
</tr>
<tr>
<td>小类型:</td>
<td><label for="label"></label>
<select name="SmailOne" id="label">
</select></td>
</tr>
</table>
<%
Pr.Close()
Set Pr = Nothing
%>
<%
Cr.Close()
Set Cr = Nothing
%>