下拉选项 2级 联动-- 服务器端(刷新)和ajax(不刷新)
来源:互联网 发布:淘宝异地客服怎么找 编辑:程序博客网 时间:2024/06/02 07:21
1. 刷新的 (这个大家都会了,我也就放个代码做个标记把,以后回头看看)
(涉及到dhtml,用随时翻手册)
简介:
2个dropdownlist ,1个省份,1个城市
当省份的dropdownlist 改变时候,将它的 AutoPostBack="True" 就可以动态刷新第二个了
(如果ajax get 方法传参是中文参数的话,可以加上 escape( a))
aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" >
</asp:DropDownList>
</div>
</form>
</body>
CS页面:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string a = "select * from dbo.p_te order by id asc ";
DataSet ds = DB.getDataSet(a);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "p_name";
this.DropDownList1.DataValueField = "p_name";
this.DropDownList1.DataBind();
string b = "select * from dbo.s_te where parentid =(select top 1 id from dbo.p_te order by id asc) ";
DataSet ds2 = DB.getDataSet(b);
this.DropDownList2.DataSource = ds2;
this.DropDownList2.DataTextField = "value";
this.DropDownList2.DataValueField = "value";
this.DropDownList2.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string a = this.DropDownList1.SelectedValue.ToString().Trim();
//Response.Write("<script>alert('" + a.Length+ "');</script>");
string b = "select * from dbo.s_te where parentid =(select top 1 id from dbo.p_te where p_name=N'"+a+"') ";
DataSet ds2 = DB.getDataSet(b);
this.DropDownList2.DataSource = ds2;
this.DropDownList2.DataTextField = "value";
this.DropDownList2.DataValueField = "value";
this.DropDownList2.DataBind();
}
2. js+ajax 实现的无刷新联动 (等有空了用jquery做,那样的话更简洁)
js页面:
<script type="text/javascript" language="javascript" >
function createxmlrequest() {
try {
// Firefox, Opera 8.0+, Safari
xmlrequest = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlrequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的浏览器不支持AJAX,请换个浏览器!");
}
}
}
return xmlrequest;
}
function getdata() {
if (xmlrequest.readyState == 4 && xmlrequest.status == 200) {
var strtext = xmlrequest.responseText; //获取的数据
var abc = strtext.split(","); //数组依次保存islocked,inventory,singinventory
var coll = document.getElementById('DropDownList2');
coll.length = 0;
for (var i = 0; i < abc.length - 1; i++) {
coll.options.add(new Option(abc[i], abc[i]));
}
document.getElementById('Label1').style.display = "none";
}
else {
document.getElementById('Label1').style.display = "block";
}
}
function check() {
var thisDate = new Date(); // ajax请求有缓存,加上时间
xmlrequest = createxmlrequest();
var a = document.getElementById('DropDownList1').value;
xmlrequest.onreadystatechange = getdata;
var strurl = "ajax_liandong.aspx?a=" +escape( a); // 传中文参数
// alert(strurl);
xmlrequest.open("GET", strurl, true);
xmlrequest.send(null);
}
</script>
ajax_liandong.aspx 接受请求页面,返回数据:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["a"] != null && Request.QueryString["a"].ToString() != "")
{
string b = "select * from dbo.s_te where parentid =(select top 1 id from dbo.p_te where p_name=N'" + Request.QueryString["a"] + "') ";
DataSet ds2 = DB.getDataSet(b);
if (ds2.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
string str = ds2.Tables[0].Rows[i]["value"].ToString() + ",";
Response.Write(""+str+"");
}
}
else
{
Response.Write("'" + Request.QueryString["a"] + "'");
}
}
else
{
Response.Write("无");
}
}
- 下拉选项 2级 联动-- 服务器端(刷新)和ajax(不刷新)
- 5级下拉框不刷新联动
- ajax无刷新二级联动下拉菜单
- AJAX无刷新下拉框联动
- AJAX无刷新下拉框联动
- 用ajax、JSP和Servlet实现多级下拉菜单无刷新联动
- AJAX+ASP.NET无刷新二级联动省市下拉列表
- jQuery Ajax实现下拉框无刷新联动
- MVC 页面实现ajax刷新联动下拉列表
- 不刷新当前页面,而多个下拉框联动
- 无刷新三级联动(ajax)
- 无刷新省级三级联动(ajax)
- 菜鸟学Java(七)——Ajax+Servlet实现无刷新下拉联动
- 无刷新联动下拉框
- DropDownList联动不刷新(二级)
- IOS数据刷新(上拉刷新和下拉刷新)
- ajax无刷新联动DropDownList
- ajax dropdownlist无刷新联动
- 影响SQL server性能的关键三个方面
- 如何在IE窗体关闭之前执行一个操作,使Session消失
- SQL临时表INSERTED和DELETED
- linux配置java环境变量(详细)
- idea intellj 速度 加快的做法
- 下拉选项 2级 联动-- 服务器端(刷新)和ajax(不刷新)
- Oracle SQL优化技巧
- ASP调用带参数存储过程的几种方式
- MyEclipse 导入cvs项目的java文件中文comments 为乱码,
- xmltocache C#
- js字符串数组传入Com组件里
- 让Windows XP自动登录的两种方法
- 第一次写博
- Google:没有必要升级至Office 2010