下拉输出

时间:2011-02-21 21:30:26

标签: c# asp.net

我使用以下代码根据第一个下拉列表中的选择填充第二个下拉列表。首先,从第一个下拉列表中选择一个项目,然后如果已将成员分配给该项目,则第二个项目将填充迭代。此时,如果未将成员分配给项目,则第二个组合框显示空白值。如果是这样的话,我想做的就是用一条消息填充它 - 例如: - 你不是这个项目的成员 - 。谢谢!

 protected void fillIterations()
 {
    selected_project = selectproject.SelectedValue;

    SqlConnection conn = new SqlConnection(GetConnectionString());
    conn.Open();

    cmd3 = new SqlCommand("SELECT DISTINCT(CONVERT(VARCHAR(10), i.StartDate, 103) + ' - ' + CONVERT(VARCHAR(10), i.EndDate, 103)) AS Iteration, i.ProjectIterationID FROM Iterations i, ProjectIterationMember pim WHERE i.ProjectIterationID = pim.ProjectIterationID AND i.ProjectID = '" + selected_project + "' AND pim.MemberID = '" + mem_id + "'", conn);

    cmd3.CommandType = CommandType.Text;

    SqlDataAdapter da3 = new SqlDataAdapter(cmd3);

    DataSet ds3 = new DataSet();

    da3.Fill(ds3);

    conn.Close();

    selectiteration.DataSource = ds3;
    selectiteration.DataTextField = "Iteration";
    selectiteration.DataValueField = "ProjectIterationID";
    selectiteration.DataBind();

    //string iteration = selectiteration.SelectedValue;
    //check_iteration.Text = iteration;
}

1 个答案:

答案 0 :(得分:1)

SqlDataAdapter da3 = new SqlDataAdapter(cmd3);

DataTable dt3 = new DataTable(); // change to DataTable

da3.Fill(dt3);

conn.Close();

if (dt3.Rows.Count > 0) {

    selectiteration.DataSource = dt3;
    selectiteration.DataTextField = "Iteration";
    selectiteration.DataValueField = "ProjectIterationID";
    selectiteration.DataBind();

} else {

    selectiteration.Items.Add(
        new ListItem("You are not a member on this project.", 0));

}