总是在Button3_Click中调用Else Part [英] The always call Else Part in Button3_Click

查看:84
本文介绍了总是在Button3_Click中调用Else Part的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button3_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)row.FindControl("chk1");

        string empname = row.Cells[1].Text;
        string leaveid = row.Cells[2].Text;
        if (chk.Checked)
        {
            try
            {
                //Create sql connection and command
                string strConnect;
                strConnect = "Data Source=BALA;Initial Catalog=employees;Persist Security Info=True;User ID=sa;Password=mips123";
                SqlConnection Connection = new SqlConnection(strConnect);
                string strUpdate = "Update Leave set status = 'Approved' WHERE (LeaveID =" + leaveid + ") AND (EmpID = (Select EmpID from Emp where (Empname='" + empname + "')))";

                SqlCommand command = new SqlCommand(strUpdate, Connection);

                Connection.Open();
                command.ExecuteNonQuery();
                Connection.Close();
                Getuser1();

            }
            catch (Exception exc)
            {

            }
        }
        else
        {
            Label2.Text = "Please select atlease one record";
        }
    }
}



它总是调用Button3_Click bcoz中的Else部分如果我按下按钮3页面回发那个时间复选框未经检查,所以plz帮助我friendz


Its always calls Else part in Button3_Click bcoz if i press button3 the page was post back that time checkbox unchecked ,so plz help me friendz

推荐答案

你在逻辑中犯了一个非常简单的错误。试试这个。

问题是,如果任何行没有选中复选框,则表示您正在显示标签,而不是如果没有选中。我删除了与答案无关的代码。

You have made a very simple error in your logic. Try this.
The problem is that if any row doesn''t have the checkbox checked you are displaying the label instead of if none are checked. I''ve removed the code that isn''t relevant to the answer.
protected void Button3_Click(object sender, EventArgs e)
{
   bool isAnythingChecked = false;

   foreach (GridViewRow row in GridView1.Rows)
   {
      CheckBox chk = (CheckBox)row.FindControl("chk1");
      if (chk != null && chk.Checked)
      {
         isAnythingChecked = true;
         // Do your stuff
      }
   }
    
   if (!isAnythingChecked)
   {
      Label2.Text = "Please select atlease one record";
   } 

}


因为 chk.Checked 是始终 false

确保至少选中一个复选框。如果它仍然相同,请调试器并查看是否获得了正确的控制。
Because chk.Checked is always false.
Make sure you are checking at least one checkbox. And if it''s still the same, put a debugger and see if you are getting the correct control.


检查是否未选中复选框chk1
Check whether checkbox "chk1" is not checked

这篇关于总是在Button3_Click中调用Else Part的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆