1.Page_Load,绑定DDL1 if (!Page.IsPostBack) { //绑定DDL1 Sql语句:Select * from [yourTable] where parentID ='0_1_' }2 DDL1 AutoPostBack=true SelectedIndexChanged: 绑定DDL2,string ddl1 =DDL1.SelectedItem.Text Sql语句用 "Select [text] from [yourTable] where Text<> '"+ddl1+"' and [text] like '"+ddl1+"%'"
to acewang(大灰很) :你误会了,我是为了看清楚方便就像那样写的,其实text是没有规律的,不能用like的。
可以在第一个dropdownlist中写: private void DropDownListA_SelectedIndexChanged(object sender, System.EventArgs e) { string strSQL = "select text from yourTable where text like '%"+this.DropDownListA.SelectedValue.ToString()+"%' order by nodeid"; SqlConnection sqlConn = new SqlConnection(strConn); SqlCommand sqlComm = new SqlCommand (strSQL,sqlConn); SqlDataReader sqlRead = null; try { sqlConn.Open(); sqlRead = sqlComm.ExecuteReader(); while (sqlRead.Read() == true) { this.DropDownListB.Items.Add(sqlRead["text"].ToString()); } } catch { sqlRead.Close(); sqlConn.Close(); } finally { sqlRead.Close(); sqlConn.Close(); } }
select mytext from t_tree where (substring(right(parentid,2),1,1)= 4)
----------
B.1
B.2
B.3
B.4
关键是那个 4 是动态的,我该怎么弄呢?
用SqlDataReader可行吗?可行怎么弄?
select * from 表名 where parentid ='0_1_'
B表
select * from 表名 where parentid = @parentid @parentid传DropDownList1选择的value
dropdownlist里面不是有autopostback属性吗?把它a的设为autopostback=true
会自动提交到本页,在page_load里,根据a提交的值绑定到b,同样的方法设置b。
if (!Page.IsPostBack)
{
//绑定DDL1 Sql语句:Select * from [yourTable] where parentID ='0_1_'
}2 DDL1 AutoPostBack=true SelectedIndexChanged:
绑定DDL2,string ddl1 =DDL1.SelectedItem.Text
Sql语句用 "Select [text] from [yourTable] where Text<> '"+ddl1+"' and [text] like '"+ddl1+"%'"
private void DropDownListA_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strSQL = "select text from yourTable where text like '%"+this.DropDownListA.SelectedValue.ToString()+"%' order by nodeid";
SqlConnection sqlConn = new SqlConnection(strConn);
SqlCommand sqlComm = new SqlCommand (strSQL,sqlConn);
SqlDataReader sqlRead = null;
try
{
sqlConn.Open();
sqlRead = sqlComm.ExecuteReader();
while (sqlRead.Read() == true)
{
this.DropDownListB.Items.Add(sqlRead["text"].ToString());
}
}
catch
{
sqlRead.Close();
sqlConn.Close();
}
finally
{
sqlRead.Close();
sqlConn.Close();
} }
nodeid parentid text
1 0_1_ A
2 0_1_1_ A.1
3 0_1_1_ A.24 0_1_ B
5 0_1_4_ B.1
6 0_1_4_ B.2
7 0_1_4_ B.3
8 0_1_4_ B.4