C# 为什么我的程序没有';如果其他操作正确,则无法执行?
我在三种不同的条件下进行测试; 第一个是下拉列表的selectedIndex 1为2,文本框为空 第二个是下拉列表的selectedIndex 1是2,文本框不是空的 最后一个是选择的下拉列表索引是3 执行时,它直接进入最后一个条件,甚至不测试第一个条件C# 为什么我的程序没有';如果其他操作正确,则无法执行?,c#,asp.net,C#,Asp.net,我在三种不同的条件下进行测试; 第一个是下拉列表的selectedIndex 1为2,文本框为空 第二个是下拉列表的selectedIndex 1是2,文本框不是空的 最后一个是选择的下拉列表索引是3 执行时,它直接进入最后一个条件,甚至不测试第一个条件 protected void btnValider_Click(object sender, EventArgs e) { if (DropDown1.SelectedIndex == 2) { if (txt
protected void btnValider_Click(object sender, EventArgs e)
{
if (DropDown1.SelectedIndex == 2)
{
if (txtNvSt.Text != null)
{
con.charger("update Reparation set dateReception='" + txtDateReception.Text + "', nNouvST='" + txtNvSt.Text + "', suivieMateriel='" + txtSuivi.Text + "', statut='" + cmbStatut.SelectedValue + "' where serviceTag ='" + txtServiceTag.Text + "'", false);
con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false);
Session["ST"] = txtNvSt.Text;
Response.Redirect("NouveauMAt.aspx");
}
else
if (txtNvSt.Text == null)
{
MessageBox.Show("txtNv null");
con.charger("update Reparation set dateReception='" + txtDateReception.Text + "',suivieMateriel='" + txtSuivi.Text + "',statut='" + DropDown1.SelectedValue + "' where serviceTag ='" + txtServiceTag.Text + "'", false);
con.charger("update Materiel set reparation = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false);
}
}
else
{
con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
con.charger("insert into Stocker values('4', '" + txtServiceTag.Text + "')", false);
con.charger("update Reparation set dateReception='" + txtDateReception.Text + "',suivieMateriel='" + txtSuivi.Text + "',statut='" + cmbStatut.SelectedValue + "' where serviceTag='" + txtServiceTag.Text + "'", false);
Response.Redirect("StockHS.aspx");
}
}
ASP文本框的
Text
属性从不为空
默认值为空字符串,因此如果要检查文本框是否为空,请将条件更改为以下内容:
if (txtNvSt.Text != string.Empty)
或者,您可以使用which帐户来表示空格,根据您的情况,空格可能仍然被认为是“空的”
你的意思是这直接指向其他(我想)。你说条件如下:
最后一个是选择的下拉列表索引是3
但是,您的else
实际上并没有检查任何内容。如果第一种情况失败,即检查DropDown1.SelectedIndex==2
,则无论SelectedIndex
的值是多少,您都将始终输入此else
请记住,SelectedIndex
是零基的,因此列表中的第二项是索引1
,依此类推。调试代码并查看在条件中检查的每个属性的值。如果您希望回答,请确保提供不需要外部信息(如cmbStatut
)的示例,或为代码中使用的每个属性提供值(作为内联注释)。cmbStatut.SelectedIndex
不等于2
。如果您调试,您可以自己查看。在第一个If
语句上设置一个断点,然后自己调试。最好使用string.IsNullOrWhiteSpace
@Jodrell添加,谢谢