我要做一个dropdownlist联动,在SelectedIndexChange里写的代码,当第一个drop动的时候用代码个第二个填充内容,代码如下:
protected void DropDownListUnitList_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
String SQLconn = "select * from department where unitid="???????????
SqlCommand myCommand = new SqlCommand(SQLconn, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
myReader.Read();
DropDownList myDD = DetailsViewMove.FindControl("DropDownListDepartmentList") as DropDownList;
if (myDD != null)
{
myDD.Items.Clear();
myDD.Items.Add(new ListItem(myReader["部门名称"].ToString()));
}
}
现在请问,我的查询语句怎么写才能通过unitid来判断我第二个dropdownlist里应该填充什么?
String SQLconn = "select * from department where unitid="???????????
就是这句!!
protected void DropDownListUnitList_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
String SQLconn = "select * from department where unitid="???????????
SqlCommand myCommand = new SqlCommand(SQLconn, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
myReader.Read();
DropDownList myDD = DetailsViewMove.FindControl("DropDownListDepartmentList") as DropDownList;
if (myDD != null)
{
myDD.Items.Clear();
myDD.Items.Add(new ListItem(myReader["部门名称"].ToString()));
}
}
现在请问,我的查询语句怎么写才能通过unitid来判断我第二个dropdownlist里应该填充什么?
String SQLconn = "select * from department where unitid="???????????
就是这句!!
这个"你选择的id"怎么写才提问的... ...
第一个是单位,第二个是部门,怎么写就能根据第一个的单位ID改变第二个里面的部门了?~
你新建一个类如下:
public class clsboxOverride
{
//This class was constructed to override the fact that there no longer
//exists an itemdata property for the list boxes in my project
private string m_sID;
private string m_sName;
public clsboxOverride(string sID,string sName)
{
m_sID = sID;
m_sName = sName;
}
public string ID
{
set
{
m_sID = value;
}
get
{
return m_sID;
}
}
public string Name
{
set
{
m_sName = value;
}
get
{
return m_sName;
}
} public override string ToString()
{
return m_sName;
}
}
把这个类作为一个对象添加到DropDownListUnitList中,然后unitid就可以在selectItem中获取了
String SQLconn = "select * from department where unitid="第一个dropdownlist的值
是应该怎么写呢?~~还望赐教
string sqltr=string.format("select * from department where unitid='{0}'",第一个dropdownlist的值)
在数据库中类型为整型:
string sqltr=string.format("select * from department where unitid={0}",第一个dropdownlist的值)dropdownlist的值
如果采用数据绑定的形式获得dropdownlist的列表值,那么取 SelectedValue你的代码是用item.add获得的,取SelectedItem.Tostring 或SelectedItem.SubItems[0].Text
首先你是否把unitid这个信息存放到dropdownlist,如果没有的话,无法在selectedindexchanged事件中获得此信息。所以你首先需要在绑定此dropdownlist的时候,用此unitid去绑定其的valuemember,否则一切都白搭。---------------------------------------
同意此观点ps:CSDN怎么显示楼层?还是楼主一路数下来的
--------------------------
回答18楼.是数的.不过是从下往上数.因为能显示总层数
String SQLconn = "select * from department where unitid="???????????
改为
String SQLconn = "select * from department where unitid=" + yourList.SelectedValue.ToString()