使用C#验证复选框 [英] Validating checkbox using C#

查看:90
本文介绍了使用C#验证复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnsubmit_Click(object sender,EventArgs e)

{

try

{

String strConnString = ConfigurationManager.ConnectionStrings [ConnectionStrings]。ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

con.Open();

foreach(grdRpt.Rows中的GridViewRow行)

{

CheckBox cb =(CheckBox)row.FindControl(chkselecdata);

if(cb.Checked == true)

{

int key = Convert.ToInt32(grdRpt.DataKeys [row.RowIndex] .Value);

SqlCommand cmd = new SqlCommand(UPDATE [transact]。[transaction_item] SET Status ='Ready'where transaction_id =+ key.ToString(),con);

cmd.ExecuteNonQuery( );

ScriptManager.RegisterStartupScript(this,GetType(),showalert,alert('Successfullyfully');,true);



}





其他

{

ScriptManager.RegisterStartupScript(this,GetType(),showalert ,alert('请选择复选框');,true);



}

}

con.Close();

BindData();

}

catch(exception ex)

{



}

}





在运行模式中,gridview具有以下结构





更新(按钮)







selectdata id transaction id数量价格状态



复选框1 12345 2 5新

复选框2 42122 1 3新



当我选中gridview中的复选框并更新记录时。在数据库记录更新。但是消息显示

protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transaction_id=" + key.ToString(), con);
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert(' Updated Successfully');", true);

}


else
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Select the Checkbox');", true);

}
}
con.Close();
BindData();
}
catch (Exception ex)
{

}
}


In the run mode gridview has follows


Update (Button)



selectdata id transactionid Qty Price status

checkbox 1 12345 2 5 New
checkbox 2 42122 1 3 New

when i select the checkbox in gridview and update the record. in database record updated. but message shows

Please Select the Checkbox.


what is the mistake in my above code.

What I have tried:

protected void btnsubmit_Click(object sender, EventArgs e)
        {
            try
            {
                String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                foreach (GridViewRow row in grdRpt.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
                    if (cb.Checked == true)
                    {
                        int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
                        SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transaction_id=" + key.ToString(), con);
                        cmd.ExecuteNonQuery();
                        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert(' Updated Successfully');", true);
                       
                    }

                   
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Select the Checkbox');", true);
                        
                    }
                }
                con.Close();
                BindData();
            }
            catch (Exception ex)
            {

            }
        }


 In the run mode gridview has follows


  Update (Button)



   selectdata id  transactionid    Qty   Price status
 
   checkbox   1      12345          2      5     New
   checkbox   2      42122          1      3     New

  when i select the checkbox in gridview and update the record. in database record updated. but message shows <pre>Please Select the Checkbox.


what is the mistake in my above code.

推荐答案

嗯......你正在为所有行循环执行此操作。因此,如果你的网格中的一行没有支票,你就会得到这条消息,无论支票有多少。



如果你只想给如果没有选中行,则显示消息,然后在循环外添加bool变量,将其设置为false,如果找到已选中的行并更新它,则将其设置为true。在循环之后,检查变量并在需要时发出消息。
Well ... you are doing it in a loop for all rows. So if one row in your grid doesn't have a check, you get the message, regardless of how many do have checks.

If you only want to give the message if no row is checked, then add a bool variable outside you loop, set it to false, and set it true if you find a checked row and update it. After the loop, check the variable and issue the message if needed.


我解释了问题所在的位置如何使用C#更新数据库中gridview选中的复选框行[ ^ ],你所做的就是重新发布题。您现在已经第二次重新发布。
I explained what the problem was at How to update gridview selected checkbox row in database using C#[^], and all you did was repost the same question. You have now reposted it a second time.


这篇关于使用C#验证复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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