两个DropDownList的客户端切换
服务器代码:
private void responseJS()//输出javascrīpt
{
ArrayList arr = productDB.getBigSmallNameToArrayList();
StringBuilder sb=new StringBuilder(@"<scrīpt language = ""Javascrīpt"">
var onecount;
onecount=0;
subcat = new Array();");
for(int i=0;i<arr.Count;i++)
{
string smallName = arr[i].ToString().Substring(arr[i].ToString().IndexOf('-') + 1);
int l = arr[i].ToString().Length - smallName.Length;
string bigName = arr[i].ToString().Substring(0, l - 1);
sb.Append(@"subcat[" + i + @"] = new Array(""" + smallName + @""",""" + bigName + @""");");
}
sb.Append(@"onecount=" + arr.Count);
sb.Append(@"
function updateDropDownList2(locationid)
{
document.form1.DropDownList2.length = 0;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{ //这句不是很理解
document.form1.DropDownList2.options[document.form1.DropDownList2.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</scrīpt>");
Clientscrīpt.RegisterClientscrīptBlock(this.GetType(),"", sb.ToString());//输出javascrīpt块 位于body内
DropDownList1.Attributes.Add("onchange", "updateDropDownList2(document.form1.DropDownList1.options[document.form1.DropDownList1.selectedIndex].value);");
生成的html代码:
<scrīpt language = "Javascrīpt">
var onecount;
onecount=0;
subcat = new Array();
subcat[0] = new Array("多普达","手机品牌");
subcat[1] = new Array("索爱","手机品牌");
subcat[2] = new Array("摩托罗拉","手机品牌");
subcat[3] = new Array("3G手机","手机品牌");
subcat[4] = new Array("诺基亚","手机品牌");
subcat[5] = new Array("LG","手机品牌");
subcat[6] = new Array("三星","手机品牌");
subcat[7] = new Array("夏新","手机品牌");
subcat[8] = new Array("联想","手机品牌");
subcat[9] = new Array("康佳","手机品牌");
subcat[10] = new Array("索爱记忆棒","闪存卡/读卡器");
subcat[11] = new Array("MMC卡","闪存卡/读卡器");
subcat[12] = new Array("SD/TF卡","闪存卡/读卡器");
subcat[13] = new Array("读卡器","闪存卡/读卡器");
subcat[14] = new Array("索爱","手机耳机/蓝牙耳机");
subcat[15] = new Array("三星","手机耳机/蓝牙耳机");
subcat[16] = new Array("诺基亚","手机耳机/蓝牙耳机");
subcat[17] = new Array("摩托罗拉","手机耳机/蓝牙耳机");
subcat[18] = new Array("LG","手机耳机/蓝牙耳机");
subcat[19] = new Array("多普达","手机耳机/蓝牙耳机");
subcat[20] = new Array("摩托罗拉","手机原装电池/充电器");
subcat[21] = new Array("诺基亚","手机原装电池/充电器");
subcat[22] = new Array("三星","手机原装电池/充电器");
subcat[23] = new Array("索爱","手机原装电池/充电器");
subcat[24] = new Array("LG","手机原装电池/充电器");
subcat[25] = new Array("多普达","手机原装电池/充电器");
subcat[26] = new Array("LG","飞毛腿电池充电器系列");
subcat[27] = new Array("三星","飞毛腿电池充电器系列");
subcat[28] = new Array("诺基亚","飞毛腿电池充电器系列");
subcat[29] = new Array("摩托罗拉","飞毛腿电池充电器系列");
subcat[30] = new Array("奥林巴斯","数码相机");
subcat[31] = new Array("三星","数码相机");
subcat[32] = new Array("索尼","数码相机");
subcat[33] = new Array("佳能","数码相机");
subcat[34] = new Array("三星","MP3/MP4");
subcat[35] = new Array("纽曼","MP3/MP4");
subcat[36] = new Array("金星","MP3/MP4");
subcat[37] = new Array("索爱","MP3/MP4");
onecount=38
function updateDropDownList2(locationid)
{
document.form1.DropDownList2.length = 0;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{ //这句不是很理解
document.form1.DropDownList2.options[document.form1.DropDownList2.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</scrīpt>
大类:<select name="DropDownList1" id="DropDownList1" ōnchange="updateDropDownList2(document.form1.DropDownList1.options[document.form1.DropDownList1.selectedIndex].value);">
<option selected="selected" value="手机品牌">手机品牌</option>
<option value="闪存卡/读卡器">闪存卡/读卡器</option>
<option value="手机耳机\蓝牙耳机">手机耳机\蓝牙耳机</option>
<option value="手机原装电池\充电器">手机原装电池\充电器</option>
<option value="飞毛腿电池充电器系列">飞毛腿电池充电器系列</option>
<option value="数码相机">数码相机</option>
<option value="MP3\MP4">MP3\MP4</option>
</select>
小类:<select name="DropDownList2" id="DropDownList2">
<option value="多普达">多普达</option>
<option value="索爱">索爱</option>
<option value="摩托罗拉">摩托罗拉</option>
<option value="3G手机">3G手机</option>
<option value="诺基亚">诺基亚</option>
<option value="LG">LG</option>
<option value="三星">三星</option>
<option value="夏新">夏新</option>
<option value="联想">联想</option>
<option value="康佳">康佳</option>
</select>