在ASP.NET中如何将数据库的数据(Name)显示到DropDownList中,如Post表中有,ID,Name,Explication 5

 我来答
ebdhfaf
2011-06-11 · TA获得超过1288个赞
知道小有建树答主
回答量:1181
采纳率:0%
帮助的人:192万
展开全部
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlProvince" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlProvince_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlDist" runat="server">
</asp:DropDownList>
</div>
</form>
</body>

cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Ganged
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}

//数据绑定
private void Bind()
{
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();

//先初始化省

SqlCommand cmdPro = new SqlCommand("select ProID,ProName from T_Province", con);
SqlDataReader drPro = cmdPro.ExecuteReader();
this.ddlProvince.DataSource = drPro;
this.ddlProvince.DataTextField ="ProName";//显示值
this.ddlProvince.DataValueField = "ProID"; //显示主键
this.ddlProvince.DataBind();
drPro.Close();

//初始化市

SqlCommand cmdCity = new SqlCommand("select CityID,CityName from T_City where ProID='" + this.ddlProvince.SelectedValue + "'", con);
SqlDataReader drCity = cmdCity.ExecuteReader();
this.ddlCity.DataSource = drCity;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
drCity.Close();

//初始化县

SqlCommand cmdDist = new SqlCommand("select id,DisName from T_District where CityID='" +this.ddlCity.SelectedValue + "'",con);
SqlDataReader drDist = cmdDist.ExecuteReader();
this.ddlDist.DataSource = drDist;
this.ddlDist.DataTextField = "DisName";
this.ddlDist.DataValueField = "id";
this.ddlDist.DataBind();
drDist.Close();
}

}

protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e) //DropDownList选中(省改变)产生的事件
{
string ProID = this.ddlProvince.SelectedValue;
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();
SqlCommand cmdCity = new SqlCommand("select CityID,CityName from T_City where ProID='" + ProID + "'", con);
SqlDataReader drCity = cmdCity.ExecuteReader();
this.ddlCity.DataSource = drCity;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
drCity.Close();
con.Close();
ddlCityBind();
}
}

protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)//DropDownList选中(市改变)产生的事件
{
ddlCityBind();
}

private void ddlCityBind()
{
string CityID = this.ddlCity.SelectedValue;
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();
SqlCommand cmdDist = new SqlCommand("select id,DisName from T_District where CityID='" + CityID + "'", con);
SqlDataReader drDist = cmdDist.ExecuteReader();
this.ddlDist.DataSource = drDist;
this.ddlDist.DataTextField = "DisName";
this.ddlDist.DataValueField = "id";
this.ddlDist.DataBind();
drDist.Close();
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
光点科技
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件... 点击进入详情页
本回答由光点科技提供
chenweidi232
2011-06-10 · TA获得超过674个赞
知道小有建树答主
回答量:858
采纳率:50%
帮助的人:400万
展开全部
public void abc(){
DataTable dt = PostService.getPost();
dropdownList1.DataSource = dt;
dropdownList1.DataTextField = "Name"; //库中 Name字段
dropdownList1.DataValueField = "ID";//库中 ID 字段
dropdownList1.DataBind();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-06-10
展开全部
private void BindDrop()
{
DataSet de = dalei.GetList("");
DropDownList1.DataSource = de;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友818239c
2011-06-10 · TA获得超过361个赞
知道小有建树答主
回答量:731
采纳率:25%
帮助的人:218万
展开全部
那就将数据库中的数据取出,然后再添加到dropdowmlist控件中。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友ecf60ff
2011-06-10
知道答主
回答量:19
采纳率:0%
帮助的人:6.6万
展开全部
用sqldatasource绑定数据库
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式