简体   繁体   English

C#检查复选框是否选中(也许用户选中并取消选中了同一复选框)

[英]C# check if checkbox check(maybe user checked and unchecked a same checkbox)

i have script from asp.net, which geting a value of checkBox, and may user click and unclick same checkbox, then i would to programm check 2 time if is check then delete it a selected row. 我有来自asp.net的脚本,该脚本获得了checkBox的值,并且可能用户单击和取消单击同一复选框,然后我要编程检查2次(如果是check),然后将其删除。 the problem i have, when i check the checkbox and uncheck a same checkbox, i do not want to delete it, but it deletes, what i have to do with my code please? 我遇到的问题是,当我选中复选框并取消选中同一复选框时,我不想删除它,但是它删除了,请问我与我的代码有什么关系?

protected void btnDelete_Click(object sender, EventArgs e)
{
    string[] Checked = hfLegalRecords.Value.Split('|');

    for (int i = 0; i < Checked.Length - 1; ++i)
    {
        string[] value = Checked[i].Split(',');
        string checkbox = value[0];
        string ClientCode = value[1];
        string DebtorNumber = value[2];
        if (checkbox.Equals("true"))
            for (int j = 0; j < checkbox[i]; ++j)
            {
                LegalEvents.RemoveSelectCheckBox(ClientCode, DebtorNumber);
            }
    }

    LoadLegalRecords();
}

No idea what you're doing, but checkbox[i] is a string. 不知道您在做什么,但是checkbox[i]是一个字符串。

for (int j = 0; j < checkbox[i]; ++j)
{
    LegalEvents.RemoveSelectCheckBox(ClientCode, DebtorNumber);
}

Here you're just calling LegalEvents.RemoveSelectCheckBox(ClientCode, DebtorNumber) for each character in the string. 在这里,您只是为字符串中的每个字符调用 LegalEvents.RemoveSelectCheckBox(ClientCode, DebtorNumber)

Actually, this code won't even compile because of j < "true" is nonsense. 实际上,由于j < "true"是胡说八道,所以该代码甚至无法编译。

I am assuming you are working with a grid/table. 我假设您正在使用表格/表格。 Are you using javascript to track your CHECKs ? 您是否使用JavaScript来跟踪您的检查? or each CHECK is a postback? 还是每个CHECK都是回发?

Use Onclick javascript function oo your checkBox and if user checks it. 如果您的复选框已选中,请使用Onclick javascript函数。 Update hfLegalRecords string with "ClientCode ,DebtorNumber , check" if he UNCHECK the same add "ClientCode ,DebtorNumber , uncheck" now when user clicks DELETE button 如果用户单击DELETE按钮,现在用“ ClientCode,DebtorNumber,取消”来更新hfLegalRecords字符串,是否取消了相同的操作就添加“ ClientCode,DebtorNumber,取消选中”

read the hfLegalRecords.Value.Split('|'); 读取hfLegalRecords.Value.Split('|'); and store them in separate DataTables/Arrays (as if checked move to Table 1; if unchecked move to Table2) 并将它们存储在单独的DataTables / Arrays中(就像选中时移动到表1一样;如果未选中时移动到表2中)

Assuming you dont have duplicate ClientCodes write a small code to check the count of records in both tables per ClientCode. 假设您没有重复的ClientCode,则编写一个小代码以检查每个ClientCode在两个表中的记录数。 If you have CientCode1 Checked . 如果您已选中CientCode1。 You have 1 entry tin Table 1. IF you have ClientCode2 Checked and Unchecked you have 1 entry in both Tables. 您在表1中有1个条目。如果已选中ClientCode2和未选中ClientCode2,则两个表中都有1个条目。 If ClientCode3 is Checked, Unchecked and Checked you will have 2 entries in Table1 and 1 entry in Table2. 如果ClientCode3为Checked,Unchecked和Checked,则表1中将有2个条目,表2中将有1个条目。

Either way, you logic should be "If entries in Table1 > entries in Table2 delete that ClientCode" else don't delete. 无论哪种方式,逻辑都应该是“如果表1中的条目>表2中的条目删除该ClientCode”,否则不要删除。

This will get complicated when you have more number records on the Screen with this simple structure of coding. 通过这种简单的编码结构,当屏幕上有更多的数字记录时,这将变得很复杂。 I personally suggest to use Asp.Net grid controls. 我个人建议使用Asp.Net网格控件。 So you can get all records and verify the checked/unchecked when user click DELETE button. 因此,当用户单击DELETE按钮时,您可以获得所有记录并验证已选中/未选中。 This will save lot of time and your effort in coding. 这将节省大量时间和您的编码工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#检查是否选中了datagridview中的任何复选框 - C# Check if any checkbox in datagridview is checked 如何从XAML检查是否选中任何复选框C# - How to check from xaml if any of the checkbox is checked c# C#Checkbox.Checked属性不反映检查更改 - C# Checkbox.Checked Property Not Reflecting Check Changes 检查两个 windows forms C# 中是否至少有一个选中的复选框 - Check if there is at least one checked checkbox in two windows forms C# C#检查datagridview复选框列中已检查值的有效方法 - C# Efficient way to check datagridview checkbox column for checked values 检查是否从另一个线程C#WPF中选中了复选框 - Check if checkbox is checked from another thread C# WPF 如何检查何时在IF语句中选中CheckBox,然后在C#中执行操作 - How To Check when a CheckBox is Checked In An IF statement And then perform an action in C# 在选中/取消选中同一行的 CheckBox 上的 GridViewRow 中启用 DropDownList(使用任何 c#、jquery、javascript) - Enable a DropDownList in a GridViewRow on CheckBox checked/unchecked of the same row(using any of c#,jquery,javascript) 检查DataGridViewCheckBoxCell中是否选中了复选框 - check if a checkbox is checked or not in DataGridViewCheckBoxCell 当Checkbox选中/未选中Gridview C#时运行事件 - Run an event when Checkbox is Checked/Unchecked Gridview C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM