Code
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" style="display:none;" Text=""></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0">--请选择--</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label2" runat="server" style="display:none;" Text=""></asp:Label>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Value="0">--请选择--</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" style="display:none;" Text=""></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0">--请选择--</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label2" runat="server" style="display:none;" Text=""></asp:Label>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Value="0">--请选择--</asp:ListItem>
</asp:DropDownList>
注意以下JS的位置,必须放到form的后面,如果在前面,那么document.getElementById("Label1")将得不到值,因为在执行这JS之前根本没有Label1这标签,故浏览器是找不到的
Code
<script type="text/javascript">
var SmallClassStr = document.getElementById("Label1").innerText;
SmallClass=SmallClassStr.split(",");
Small=new Array()
for (i=0;i<SmallClass.length;i++){
Small[i]=SmallClass[i].split("-");
}
SmallCount=Small.length;
//此处实现联动
function changelocation(locationid)
{
document.form1.DropDownList2.length = 1;
var locationid=locationid;
var i;
for (i=0;i < SmallCount; i++)
{
if (Small[i][0] == locationid)
{
document.form1.DropDownList2.options[document.form1.DropDownList2.length] = new Option(Small[i][2], Small[i][1]);
}
}
document.form1.DropDownList3.options.length = 1;
}
var ThreeStr = document.getElementById("Label2").innerText;
ThreeClass=ThreeStr.split(",");
Three=new Array()
for (i=0;i<ThreeClass.length;i++){
Three[i]=ThreeClass[i].split("-");
}
ThreeCount=Three.length;
//此处实现联动
function changelocationa(locationid)
{
document.form1.DropDownList3.length = 1;
var locationid=locationid;
var i;
for (i=0;i < ThreeCount; i++)
{
if (Three[i][0] == locationid)
{
document.form1.DropDownList3.options[document.form1.DropDownList3.length] = new Option(Three[i][2], Three[i][1]);
}
}
}
</script>
<script type="text/javascript">
var SmallClassStr = document.getElementById("Label1").innerText;
SmallClass=SmallClassStr.split(",");
Small=new Array()
for (i=0;i<SmallClass.length;i++){
Small[i]=SmallClass[i].split("-");
}
SmallCount=Small.length;
//此处实现联动
function changelocation(locationid)
{
document.form1.DropDownList2.length = 1;
var locationid=locationid;
var i;
for (i=0;i < SmallCount; i++)
{
if (Small[i][0] == locationid)
{
document.form1.DropDownList2.options[document.form1.DropDownList2.length] = new Option(Small[i][2], Small[i][1]);
}
}
document.form1.DropDownList3.options.length = 1;
}
var ThreeStr = document.getElementById("Label2").innerText;
ThreeClass=ThreeStr.split(",");
Three=new Array()
for (i=0;i<ThreeClass.length;i++){
Three[i]=ThreeClass[i].split("-");
}
ThreeCount=Three.length;
//此处实现联动
function changelocationa(locationid)
{
document.form1.DropDownList3.length = 1;
var locationid=locationid;
var i;
for (i=0;i < ThreeCount; i++)
{
if (Three[i][0] == locationid)
{
document.form1.DropDownList3.options[document.form1.DropDownList3.length] = new Option(Three[i][2], Three[i][1]);
}
}
}
</script>
Code
DataTable BigDt = Product.Show_ProductBigClassList();
DropDownList1.DataSource = BigDt;
DropDownList1.DataTextField = "BigClassName";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--请选择--", "0"));
DropDownList1.Attributes.Add("onChange", "changelocation(document.form1.DropDownList1.options[document.form1.DropDownList1.selectedIndex].value)");
DropDownList2.Attributes.Add("onChange", "changelocationa(document.form1.DropDownList2.options[document.form1.DropDownList2.selectedIndex].value)");
DataTable SmallDt = Product.Show_ProductSmallList();
foreach (DataRow row in SmallDt.Rows)
{
Label1.Text = Label1.Text + row["Pid"] + "-" + row["Id"] + "-" + row["SmallClassName"] + ",";
}
DataTable ThreeDt = Product.GetThreeClass();
foreach (DataRow row in ThreeDt.Rows)
{
Label2.Text = Label2.Text + row["ParentId"] + "-" + row["Id"] + "-" + row["ClassName"] + ",";
}
DataTable BigDt = Product.Show_ProductBigClassList();
DropDownList1.DataSource = BigDt;
DropDownList1.DataTextField = "BigClassName";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--请选择--", "0"));
DropDownList1.Attributes.Add("onChange", "changelocation(document.form1.DropDownList1.options[document.form1.DropDownList1.selectedIndex].value)");
DropDownList2.Attributes.Add("onChange", "changelocationa(document.form1.DropDownList2.options[document.form1.DropDownList2.selectedIndex].value)");
DataTable SmallDt = Product.Show_ProductSmallList();
foreach (DataRow row in SmallDt.Rows)
{
Label1.Text = Label1.Text + row["Pid"] + "-" + row["Id"] + "-" + row["SmallClassName"] + ",";
}
DataTable ThreeDt = Product.GetThreeClass();
foreach (DataRow row in ThreeDt.Rows)
{
Label2.Text = Label2.Text + row["ParentId"] + "-" + row["Id"] + "-" + row["ClassName"] + ",";
}