ASP.Net里面怎样实现两个dropdownlist联动
发布网友
发布时间:2022-04-20 22:33
我来回答
共6个回答
热心网友
时间:2022-04-21 00:02
百度HI聊..
dropdownlist1有个 SelectedIndexChanged事件,可以在这个事件里面来写代码,获取当前选择的是 a,b或者是c 然后,根据abc来设置dropdownlist2
注意,需要把dropdownlist1的AutoPostBack设置为true
热心网友
时间:2022-04-21 01:20
在head中加入
<script language=javascript>
function load(state)
{
var drp2 = document.getElementById("ddllei2");
for (i = drp2.length; i >= 0; i--)
{
drp2.options.remove(i);
}
var newOption = document.createElement("OPTION");
newOption.text="选择小类";
newOption.value="";
drp2.options.add(newOption);
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "getlei.aspx?id="+state, false);
//getlei.aspx返回为要得到第二个dropdownlist中的内容
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
//alert(result);
var naItems = oDoc.selectNodes("//NewDataSet/table/id"); //id对应你的列名
var idItems = oDoc.selectNodes("//NewDataSet/table/leibie"); //节点名大小写区分 leibie对应你的列名
var item;
var id;
for (item = naItems.nextNode(),id=idItems.nextNode(); item&&id; item = naItems.nextNode(),id=idItems.nextNode())
{
var nastr = item.nodeTypedValue;
var idstr = id.nodeTypedValue;
var newOption = document.createElement("OPTION");
newOption.text=idstr;
newOption.value=nastr;
drp2.options.add(newOption);
}
}
</script>
getlei.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
public partial class cofi_adm_zhanshi_getlei : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = DbHelper.DbHelperAccess.GetDataSet("select * from s_zhanshilei where parentid=" + Request.QueryString["id"] + "");
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.IndentChar = ' ';
writer.WriteStartDocument();
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
}
}
含有两个dropdownlist的页面
<tr runat=server id=trlei >
<td width="10%" align="right">类别:</td>
<td>
<asp:DropDownList ID=ddllei runat=server></asp:DropDownList>
<asp:DropDownList CssClass="select" ID=ddllei2 runat=server ></asp:DropDownList>
</td>
</tr>
后台cs代吗
List<s_zhanshilei> plist = s_zhanshilei_db.DtlList("select * from s_zhanshilei where parentid=0");
for (int i = 0; i < plist.Count; i++)
{
ddllei.Items.Add(new ListItem(plist[i].leibie.ToString(), plist[i].id.ToString()));
}
ddllei.Items.Insert(0, new ListItem("请选择大类", ""));
ddllei2.Items.Insert(0, new ListItem("请选择小类", ""));
this.ddllei.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
热心网友
时间:2022-04-21 02:55
AutoPostBack =true
你可以直接用绑定数据··然后把绑定的数据id 传到另外一个dropdownlist就行了
热心网友
时间:2022-04-21 04:46
这个只要在A的selectedIndexChanged事件中,然后根据A的Text动态添加B的内容,A的AutoPostBack要设置成true。BC同样。
想不刷新页面用Ajax。
热心网友
时间:2022-04-21 06:54
AJAX实现联动。
或者直接 刷新 ,AutoPostBack="true"