如果ELSE条件不正常

时间:2016-04-29 10:16:59

标签: c# asp.net if-statement

我有多选DROPDOWNLIST来选择项目。请参阅下面的Dropdownlist

[![下拉列表] [1] [1]

我正在做的是,我从列表中选择2个项目。其中一个PROCESSED和另一个PENDING

所以这里发生的错误是,当条件为PROCESSED时,它正常工作并进入IF条件,但第二次是PENDING但仍然进入{{1}条件。

IF

这是标记:

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
    using (SqlCommand sqcmd = new SqlCommand("select  month(a.dt_of_leave)month, year(a.dt_of_leave)year   " +
        "from emp_mst a where month(a.dt_of_leave) >= month(getdate())-1  and   " +
        "year(a.dt_of_leave)= case when  month(getdate())=1   " +
        "then year(getdate())-1 else year(getdate()) end  " +
        "and emp_card_no IN (" + str_emp_sel + ") order by emp_name", conn))
    {
        SqlDataAdapter damonthyear = new SqlDataAdapter(sqcmd);
        damonthyear.Fill(dtspmonthyear);

        for (i = 0; i < dtspmonthyear.Rows.Count; i++)
        {
            if (cmbEmp_Name.SelectedItem.Text.Contains("PROCESSED") == true)
            {
                //CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
            }
            else
            {
                // not going in else for `PENDING`
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

尝试类似的事情:

for (i = 0; i < dtspmonthyear.Rows.Count; i++)
{
    foreach (var item in cmbEmp_Name.Items)
    {
        if (item.Selected)
        {
            if (item.Text.Contains("PROCESSED"))
            {
                //CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
            }
            else
            {
                // not going in else for `PENDING`
            }
        }
    }
}

答案 1 :(得分:1)

您显然已下载some control from the web。其文件说明:

  

DropDownCheckBoxes是一个直接从标准ASP.NET CheckBoxList控件继承的ASP.NET服务器控件。

很酷,所以我们可以在网上搜索我们想要做的事情:“ASP.NET CheckBoxList get selected items”,它产生Q&amp;类似于How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart,{{ 3}}等等:

foreach (ListItem item in dropDownCheckBoxList.Items)
{
    if (item.Selected)
    {
        // Do what you want to do
    }
}