如何使用ASP.NET仅检查gridview中的一个复选框 [英] How to check only one check box in gridview using ASP.NET

查看:56
本文介绍了如何使用ASP.NET仅检查gridview中的一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  btnView_Click(对象发​​件人,EventArgs e)
{
foreach (GridViewRow行 in GridLeaveApproved.Rows)
{
CheckBox ch =(CheckBox)row.FindControl( CheckBox1);
if (ch.Checked)
{
// < span class =code-comment>标签lblleaveid =(标签)row.FindControl(lblLid);

// int lid = Convert.ToInt32(lblleaveid.Text);
// 标签lbllid =(标签)row.FindControl(lblLId);
// int lid = Convert.ToInt32(lbllid.Text);
Label lblEid =(Label)row.FindControl( lblEmpId);
string eid = lblEid.Text;
cmd = new MySqlCommand( 选择*来自empleaves,其中EmpId = @ eid,conn);
cmd.Parameters.AddWithValue( @ eid,eid);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, newleave);
GridLeavesView.DataSource = ds.Tables [ newleave];
GridLeavesView.DataBind();
}
}
GridLeavesView.Visible = true ;
}









i只想查看一个复选框。我正在尝试选择两个复选框,在单击查看按钮后显示错误消息(仅选择一个复选框)。





请帮助我朋友..



我尝试了什么:



protected void btnView_Click (对象发送者,EventArgs e)

{

foreach(GridLeaveApproved.Rows中的GridViewRow行)

{

CheckBox ch =(CheckBox)row.FindControl(CheckBox1);

if(ch.Checked)

{

// Label lblleaveid =(标签)row.FindControl(lblLid);

// int lid = Convert.ToInt32(lblleaveid.Text);

//标签lbllid =(标签)row.FindControl(lblLId);

// int lid = Convert.ToInt32(lbllid.Text);

Label lblEid =(Label)row.FindControl( lblEmpId);

string eid = lblEid.Text;

cmd = new MySqlCommand(select * from empleaves,其中EmpId = @ eid,conn);

cmd.Parameters.AddWithValue( @eid,eid);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);

DataSet ds = new DataSet();

da。填充(ds,newleave);

GridLeavesView.DataSource = ds.Tables [newleave];

GridLeavesView.DataBind();

}

}

GridLeavesView.Visible = true;

}

解决方案

< blockquote>由于你已经标记了Asp.net和C#,我使用c#给你解决方案。

我希望你有一个标签来显示错误信息。



 受保护  void  btnView_Click( object  sender,EventArgs e)
{
lblMessage.Text = ;
int count = 0 ;
foreach (GridViewRow行 GridLeaveApproved.Rows)
{
CheckBox ch =(CheckBox)row.FindControl( CheckBox1);
if (ch.Checked)
{
count ++;
if (count > 1
{
lblMessage.Text = 请只选择一行;
return ;
}
}
}

foreach (GridViewRow行 in GridLeaveApproved.Rows)
{
CheckBox ch =(CheckBox)row.FindControl( CheckBox1);
if (ch.Checked)
{
// < span class =code-comment>标签lblleaveid =(标签)row.FindControl(lblLid);

// int lid = Convert.ToInt32(lblleaveid.Text);
// 标签lbllid =(标签)row.FindControl(lblLId);
// int lid = Convert.ToInt32(lbllid.Text);
Label lblEid =(Label)row.FindControl( lblEmpId);
string eid = lblEid.Text;
cmd = new MySqlCommand( 选择*来自empleaves,其中EmpId = @ eid,conn);
cmd.Parameters.AddWithValue( @ eid,eid);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, newleave);
GridLeavesView.DataSource = ds.Tables [ newleave];
GridLeavesView.DataBind();
}
}
GridLeavesView.Visible = true ;


}


你也可以这样做..



  foreach (DataGridViewRow行  GridLeaveApproved.Rows) 
{
DataGridViewCheckBoxCell ch =(DataGridViewCheckBoxCell)row.Cells [ 1 ];
if (ch.Selected == true
{
// 执行代码检查
}
else
{
// 如果未检查则执行代码
}
}


protected void btnView_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridLeaveApproved.Rows)
            {
                CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
                if (ch.Checked)
                {
                    //Label lblleaveid = (Label)row.FindControl("lblLid");
                    //int lid = Convert.ToInt32(lblleaveid.Text);
                    //Label lbllid = (Label)row.FindControl("lblLId");
                    //int lid = Convert.ToInt32(lbllid.Text);
                    Label lblEid = (Label)row.FindControl("lblEmpId");
                    string eid = lblEid.Text;
                    cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
                    cmd.Parameters.AddWithValue("@eid", eid);
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "newleave");
                    GridLeavesView.DataSource = ds.Tables["newleave"];
                    GridLeavesView.DataBind();
                }
            }
            GridLeavesView.Visible = true;
        }





i want to check only one checkbox. i am trying to select two checkboxes show error message like(only select one checkbox) when after clicking View Button.


please help me friends..

What I have tried:

protected void btnView_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridLeaveApproved.Rows)
{
CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
if (ch.Checked)
{
//Label lblleaveid = (Label)row.FindControl("lblLid");
//int lid = Convert.ToInt32(lblleaveid.Text);
//Label lbllid = (Label)row.FindControl("lblLId");
//int lid = Convert.ToInt32(lbllid.Text);
Label lblEid = (Label)row.FindControl("lblEmpId");
string eid = lblEid.Text;
cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
cmd.Parameters.AddWithValue("@eid", eid);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "newleave");
GridLeavesView.DataSource = ds.Tables["newleave"];
GridLeavesView.DataBind();
}
}
GridLeavesView.Visible = true;
}

解决方案

Since you have tagged Asp.net and C#, i am giving you the solution using c#.
and i hope you will be having a label to display the error message.

protected void btnView_Click(object sender, EventArgs e)
       {
           lblMessage.Text = "";
           int count = 0;
           foreach (GridViewRow row in GridLeaveApproved.Rows)
           {
               CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
               if (ch.Checked)
               {
                   count++;
                   if (count > 1)
                   {
                       lblMessage.Text = "Please select only one row";
                       return;
                   }
               }
           }

           foreach (GridViewRow row in GridLeaveApproved.Rows)
           {
               CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
               if (ch.Checked)
               {
                   //Label lblleaveid = (Label)row.FindControl("lblLid");
                   //int lid = Convert.ToInt32(lblleaveid.Text);
                   //Label lbllid = (Label)row.FindControl("lblLId");
                   //int lid = Convert.ToInt32(lbllid.Text);
                   Label lblEid = (Label)row.FindControl("lblEmpId");
                   string eid = lblEid.Text;
                   cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
                   cmd.Parameters.AddWithValue("@eid", eid);
                   MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                   DataSet ds = new DataSet();
                   da.Fill(ds, "newleave");
                   GridLeavesView.DataSource = ds.Tables["newleave"];
                   GridLeavesView.DataBind();
               }
           }
           GridLeavesView.Visible = true;


       }


You can also perform like this..

foreach (DataGridViewRow row in GridLeaveApproved.Rows)
        {
            DataGridViewCheckBoxCell ch = (DataGridViewCheckBoxCell)row.Cells[1];
            if (ch.Selected == true)
            {
                //do code if it checked
            }
            else
            {
                //do code if it not checked
            }
        }


这篇关于如何使用ASP.NET仅检查gridview中的一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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