c# .net 下拉菜单

2025-02-26 08:16:09
推荐回答(5个)
回答1:

这是一个二级联动的下拉框,你可以用Ajax实现无刷新二级联动下拉框更好:

制作DropDownList无刷新ajax二级联动效果:

begin:

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

页面主要代码:

Width="200" AutoPostBack="True">

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

程序主要代码:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindDrop();

}

}

private void BindDrop()

{

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

string sqlStr = "select * from schoolname";

DataTable dt=DataBase.GetTable(sqlStr);

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

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

回答2:

这个简单,有很多种实现方式。一种在下拉菜单的对应选项中的Value是 地址
下拉菜单的change事件中获取选中的值,对asp:Literal 进行赋值
一种是下拉菜单的change事件中获取选中的值,然后根据选中的值遍历数据库数据,对asp:Literal 进行赋值

我觉得不用使用Ajax,太麻烦了,而且你用的是服务端控件。如果不懂得话可以给我发邮件jiale00700@163.com

回答3:

下拉菜单有一个 选中事件,
在事件里写.
假如页面有一个lable
那就写 lable.text=DropDownList1.SelectedItem.Value;

在下来菜单控件别忘记加 autopost=true

回答4:

这里有一个多级联动下拉select菜单
还可以自己设置默认显示值
里面有代码可以参考

回答5:

晕 ~~用三元