在ASP.NET中如何将数据库的数据(Name)显示到DropDownList中,如Post表中有,ID,Name,Explication 5
5个回答
展开全部
<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();
}
}
}
}
<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 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
public void abc(){
DataTable dt = PostService.getPost();
dropdownList1.DataSource = dt;
dropdownList1.DataTextField = "Name"; //库中 Name字段
dropdownList1.DataValueField = "ID";//库中 ID 字段
dropdownList1.DataBind();
}
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"));
}
{
DataSet de = dalei.GetList("");
DropDownList1.DataSource = de;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那就将数据库中的数据取出,然后再添加到dropdowmlist控件中。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用sqldatasource绑定数据库
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询