ajax 如何使 dropdownlist 无刷新,asp.net DropDownList无刷新ajax二级联动实现详细过程...

本文适合新手,介绍如何在ASP.NET中创建无刷新的AJAX二级联动DropDownList。通过数据库操作,实现当选择第一个DropDownList的选项时,第二个DropDownList自动更新相关数据,无需页面刷新。
摘要由CSDN通过智能技术生成

本例只适合新手制作DropDownList无刷新ajax二级联动效果:

begin:

数据库实现,添加两表如图:表1,pingpai,表2,type,具体数据库实现看自己的理解:

页面主要代码:

Width="200" AutoPostBack="True">

//以上代码实现ajax的无刷新效果

程序主要代码:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindDrop();

}

}

private void BindDrop()

{

//将数据捆绑到下拉列表中

string sqlStr = "select * from pingpai";

DataTable dt=DataBase.GetTable(sqlStr);

DropDownList1.DataTextField = "pingpai"; //设置列表显示的字

DropDownList1.DataValueField = "typeid"; //设置列表提交后获得的字段,自己理解为隐藏绑定数据

DropDownList1.DataSource = dt.DefaultView;

DropDownList1.DataBind();

DropDownList1.Items.Insert(0, new ListItem("请选择车子品牌", ""));//第一项中加入内容,重点是绑定后添加

DropDownList2.Items.Insert(0, new ListItem("请选择车子品牌型号", ""));//第一项中加入内容,重点是绑定后添加

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

int typeid = Convert.ToInt32(DropDownList1.SelectedValue);//页面加载后DropDownList1.DataValueField隐藏绑定的数据,后边根据它查询DropDownList2要显现的数据

string sqlStr = "select * from type where typeid="" + typeid + """;

DataTable dt = DataBase.GetTable(sqlStr);

DropDownList2.DataTextField = "type"; //设置DropDownList1事件SelectedIndexChanged改变后DropDownList2列表显示的数据

DropDownList2.DataSource = dt.DefaultView;

DropDownList2.DataBind();;

}

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值