如何从一个下拉列表调用数据到另一个 [英] how call data from one dropdownlist to another
本文介绍了如何从一个下拉列表调用数据到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在选择dropdwonlist1时在dropdownlist2中设置数据.
例如,我想在选择州时列出所有城市.单击提交按钮后,我的代码可以正常工作.
代码是
受保护的 无效 DropDownList2_SelectedIndexChanged(对象发件人,EventArgs e)
{
con.Open();
SqlCommand cmd = 新 SqlCommand(" + DropDownList2.SelectedItem.Text + '",con);
dr = cmd.ExecuteReader();
DropDownList3.DataSource = dr;
DropDownList3.DataValueField = " ;
DropDownList3.DataBind();
DropDownList3.SelectedIndex = -1;
con.Close();
}
受保护的 无效 Button1_Click(对象发件人,EventArgs e)
{
字符串 st = " ;
如果(IsPostBack)
{
尝试
{
SqlCommand cmd1 = 新建 SqlCommand(" + DropDownList3.SelectedItem.Text + '",con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
如果(dr.Read())
{
st = dr [ 0 ].ToString();
dr.Close();
}
SqlCommand cmd = 新 SqlCommand(" + st + ',' + DropDownList1.SelectedItem.Text + " + DropDownList2.SelectedItem.Text + " ','" + DropDownList3.SelectedItem.Text + + TextBox1.Text + " " ,con);
cmd.ExecuteNonQuery();
}
捕获(SqlException ex)
{
}
最终
{
con.Close();
}
}
}
解决方案
单击提交按钮后,我的代码可以正常工作
将buttonclick
事件中的代码替换为DropDownList1_SelectedIndexChanged
事件.您会完成的.
在这里有点迷路了..
dropdownlist1是您的状态下拉列表
和下拉列表2是您的城市下拉列表吗?
那么为什么不在页面加载时填充状态下拉列表呢?
并且在您的州下拉列表的selectedindexchanged事件中,您可以根据所选州来填充城市下拉列表.下拉列表2,仅此而已...
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM AddedItem where Item_Type=''" + DropDownList1.SelectedItem.Text + "''", con); dr = cmd.ExecuteReader(); DropDownList2.DataSource = dr; DropDownList2.DataValueField = "Item_Name"; DropDownList2.DataBind(); DropDownList2.SelectedIndex = -1; con.Close(); }
DropDownList1_SelectedIndexChanged
如果未触发,则设置下拉列表属性"autopostback = true"
How to set data in dropdownlist2 on selection of dropdwonlist1.
For example I want to list of all city on selection of state.My code is working after click on submit button.
code is
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM AddedItem where Item_Type='" + DropDownList2.SelectedItem.Text + "'", con);
dr = cmd.ExecuteReader();
DropDownList3.DataSource = dr;
DropDownList3.DataValueField = "Item_Name";
DropDownList3.DataBind();
DropDownList3.SelectedIndex = -1;
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string st = "";
if (IsPostBack)
{
try
{
SqlCommand cmd1 = new SqlCommand("SELECT Item_Id from AddedItem where Item_Name='" + DropDownList3.SelectedItem.Text + "'", con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
if (dr.Read())
{
st = dr[0].ToString();
dr.Close();
}
SqlCommand cmd = new SqlCommand("INSERT INTO IssuedItem VALUES('" + st + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','"+DropDownList3.SelectedItem.Text+"','"+TextBox1.Text+"','"+TextBox2.Text+"')", con);
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
}
finally
{
con.Close();
}
}
}
解决方案
My code is working after click on submit button
Replace the code which is inbuttonclick
event toDropDownList1_SelectedIndexChanged
event. You will be done.
am a bit lost here..
dropdownlist1 is your state dropdown
and dropdownlist 2 is your city dropdown right?
so why dont you populate your state dropdownlist on page load
and in the selectedindexchanged event of your state dropdownlist u can populate your city dropdownlist based on the state selected
according to my understanding what you have to do is repeating the same logic that you did for drop down list 2 and nothing more...
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM AddedItem where Item_Type=''" + DropDownList1.SelectedItem.Text + "''", con); dr = cmd.ExecuteReader(); DropDownList2.DataSource = dr; DropDownList2.DataValueField = "Item_Name"; DropDownList2.DataBind(); DropDownList2.SelectedIndex = -1; con.Close(); }
DropDownList1_SelectedIndexChanged
if not fired then set the drop down list property "autopostback= true"
这篇关于如何从一个下拉列表调用数据到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文