我有两个页面,一个是管理员,另一个是教师。管理员根据教师分配科目。学院只想选择所需的小时数。我的问题是,我不想对已禁用的复选框执行更新查询。我已经尝试了以下代码,但它也是为禁用复选框更新。
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString))
{
foreach (GridViewRow r in Gv1.Rows){
if ((((
(((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == true
|| ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == false)
|| (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == false)
))))
{
SqlCommand comm;
CheckBox aa;
bool hour1 = (r.FindControl("chkHour1") as CheckBox).Checked;
bool hour2 = (r.FindControl("chkHour2") as CheckBox).Checked;
bool hour3 = (r.FindControl("chkHour3") as CheckBox).Checked;
bool hour4 = (r.FindControl("chkHour4") as CheckBox).Checked;
bool hour5 = (r.FindControl("chkHour5") as CheckBox).Checked;
string subject1 = ddlsubj.SelectedValue;
string subject2 = ddlsubj.SelectedValue;
string subject3 = ddlsubj.SelectedValue;
string subject4 = ddlsubj.SelectedValue;
string subject5 = ddlsubj.SelectedValue;
string datedif = r.Cells[0].Text;
aa=(CheckBox)sender;
con2.Open();
comm = new SqlCommand();
comm.Connection = con2;
comm.CommandType = CommandType.Text;
if (aa.Enabled==false)
{
comm = new SqlCommand("UPDATE test
SET aa=false
when subject1=CASE WHEN (@hour1)= 'false' THEN NULL
ELSE @subject1 END,
subject2=CASE WHEN (@hour2)= 'false' THEN NULL
ELSE @subject2 END,
subject3=CASE WHEN (@hour3)= 'false' THEN NULL
ELSE @subject3 END,
subject4=CASE WHEN (@hour4)= 'false' THEN NULL
ELSE @subject4 END,
subject5=CASE WHEN (@hour5)='false' THEN NULL
ELSE @subject5 END,
hour1 = CASE WHEN (@hour1)= 'false' THEN 'false'
ELSE 'true' END,
hour2 = CASE WHEN (@hour2)= 'false' THEN 'false'
ELSE 'true' END,
hour3 = CASE WHEN (@hour3)= 'false' THEN 'false'
ELSE 'true' END ,
hour4 = CASE WHEN (@hour4)= 'false' THEN 'false'
ELSE 'true' END ,
hour5 = CASE WHEN (@hour5)= 'false' THEN 'false'
ELSE 'true' END where datedif=@datedif", con2);
}
else
{
comm = new SqlCommand("update test set aa=true
when subject1=CASE WHEN (@hour1)= 'false' THEN NULL
ELSE @subject1 END,
subject2=CASE WHEN (@hour2)= 'false' THEN NULL
ELSE @subject2 END,
subject3=CASE WHEN (@hour3)= 'false' THEN NULL
ELSE @subject3 END,
subject4=CASE WHEN (@hour4)= 'false' THEN NULL
ELSE @subject4 END,
subject5=CASE WHEN (@hour5)='false' THEN NULL
ELSE @subject5 END,
hour1 = CASE WHEN (@hour1)= 'false' THEN 'false'
ELSE 'true' END,
hour2 = CASE WHEN (@hour2)= 'false' THEN 'false'
ELSE 'true' END,
hour3 = CASE WHEN (@hour3)= 'false' THEN 'false'
ELSE 'true' END ,
hour4 = CASE WHEN (@hour4)= 'false' THEN 'false'
ELSE 'true' END ,
hour5 = CASE WHEN (@hour5)= 'false' THEN 'false'
ELSE 'true' END where datedif=@datedif", con2);
}
comm.Parameters.AddWithValue("@subject1",subject1);
comm.Parameters.AddWithValue("@subject2", subject2);
comm.Parameters.AddWithValue("@subject3", subject3);
comm.Parameters.AddWithValue("@subject4", subject4);
comm.Parameters.AddWithValue("@subject5", subject5);
comm.Parameters.AddWithValue("@hour1", hour1);
comm.Parameters.AddWithValue("@datedif", datedif);
comm.Parameters.AddWithValue("@hour2", hour2);
comm.Parameters.AddWithValue("@hour3", hour3);
comm.Parameters.AddWithValue("@hour4", hour4);
comm.Parameters.AddWithValue("@hour5", hour5);
comm.ExecuteNonQuery();
con2.Close();
}
}
}
答案 0 :(得分:1)
您可能希望首先删除IF语句,因为它不需要。正如其他人所说的那样,正在检查两个真正的OR(||)假,它总是正确的。
其次,看不到你检查的任何地方.Enabled == False 这可能是你正在寻找的。 p>
答案 1 :(得分:1)
根据您的代码,如果禁用复选框,则执行一个查询,如果启用了复选框,则执行另一个查询
if (aa.Enabled==false)
{
}
else{
}
如果您不想在禁用的复选框上执行,请删除部分
if (aa.Enabled==false)
{
}