来自数据库的 ASP 下拉列表数据绑定
Posted
技术标签:
【中文标题】来自数据库的 ASP 下拉列表数据绑定【英文标题】:ASP dropdownlist databind from database 【发布时间】:2012-07-21 12:40:06 【问题描述】:我正在使用 2 个下拉列表。国家第一,州第二。如果我从 1ft 下拉列表中选择 India,第二个会自动从数据库中自动绑定印度的所有州。
我已将country_tbl
用于国家,并与第一个下拉列表和india_tbl
、us_tbl
、sri_tbl
绑定用于这些国家/地区的绑定状态。
请帮助我。我该怎么办?
我的代码如下:
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
method1();
protected void method1()
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from country";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataSource = dr;
DropDownList1.DataBind();
con.Close();
dr.Close();
protected void methodInd()
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from india";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
protected void methodpak()
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from pakistan";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
protected void methodsri()
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from srilanka";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
protected void submit_Click(object sender, EventArgs e)
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
if (DropDownList1.SelectedItem.Text=="india")
methodInd();
else if (DropDownList1.SelectedItem.Text=="pakistan")
methodpak();
else if (DropDownList1.SelectedItem.Text=="srilanka")
methodsri();
【问题讨论】:
每个国家都有不同的表格?你确定你做对了吗?您可以将所有州保存在一个表中,并使用 CountryID 列来标识this states belongs to which country
。您可以像SELECT ID,NAME FROM STATE WHERE COUNTRY_ID=theCountryId
一样查询它
【参考方案1】:
您的方法是错误的,您不应该为每个国家/地区的州提供单独的表格,因此您可以有一个查询和一种方法来绑定到您的州列表
将架构更改为
CREATE TABLE Country
(
id int,
country_name,
primary key(id)
)
Country_State
(
id int,
state_name,
country_id,
primary key(id)
)
country_id 是链接回该国家/地区的外键
country
------------------------
id Name
------------------------
1 India
2 Pakistan
country_state
-----------------------------------
id Name country_id
------------------------------------
1 Delhi 1
2 Bangladesh 1
3 Some Indians 1
4 S1_Pakistan 2
5 S2_Pakistan 2
您的查询
Select Id, Name from Country
Select Id, Name From country_states Where country_id = @id
只调用一种方法来绑定所有状态
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
string country_id = DropDownList1.SelectedValue;
BindStatesByCountry(country_id);
protected void methodsri(string countryId) //change this to BindStatesByCountry
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
con.Open();
string s2 = "select * from country_states where country_id=@countryId";
SqlCommand cmd = new SqlCommand(s2, con);
cmd.Parameters.AddWithValue("@countryId", countryId);
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
希望对你有帮助
【讨论】:
【参考方案2】:你想实现一个主细节模式,所以看这个网址: http://msdn.microsoft.com/en-us/library/aa581789.aspx 更喜欢存储过程而不是 sql 注入 最好的尊重
【讨论】:
以上是关于来自数据库的 ASP 下拉列表数据绑定的主要内容,如果未能解决你的问题,请参考以下文章
用 jQuery 设置 ASP.NET 数据绑定下拉列表的值
无法绑定来自 jquery 数据库中 json 值的下拉列表值
来自数据库的数据不能显示在 ASP.NET MVC 的下拉列表中