简体   繁体   English

比较数据列表中的行的最佳方法是什么

[英]What is the best way to compare rows in a datalist

The general question I am asking is what is the best way to compare rows in a datalist. 我要问的一般问题是比较数据列表中行的最佳方法是什么。 The code I have is about a datalist that contains a textbox and two buttons, one submit, and one clear. 我拥有的代码是关于一个数据列表的,该数据列表包含一个文本框和两个按钮,一个提交,一个清除。 When the user clicks submit, I am trying to compare the textbox with the othter rows. 当用户单击提交时,我正在尝试将文本框与其他行进行比较。 The button click is a command and I am passing the ItemIndex as the command argument so I do know what row the button click is happening on. 按钮单击是命令,我将ItemIndex作为命令参数传递,因此我确实知道按钮单击发生在哪一行。 I am using a foreach loop to go through each row. 我正在使用foreach循环遍历每一行。

The following code is my foreach loop that is inside of my click event 以下代码是我click事件中的foreach循环

int giftCount = 0;

foreach(DataListItem dli in dlGiftCode)
{
    bool isCurrentRow = dli.ItemIndex.ToString() == e.CommandArgument.ToString() ? true:false;
    int currentRow = Convert.ToInt32(e.CommandArgument);
    TextBox txtCardCode = (TextBox)giftcode.FindControl("txtCardCode");
    string currentCode = txtCardCode.Text;

    for(int x = 0; x < dlGiftCode.Items.Count; x++)
    {
        if(currentCode == txtCardCode.Text && !isCurrentRow)
            giftCount++;
    }

    if(giftCount <= 1)
    { 
        //Continue on
    }
    else
    {
        //show message
    }
}

The for loop is my failed attempt to find duplicates. for循环是我无法找到重复项的尝试。 I can see why it does not work but I just can't seem to get the correct logic on my issue. 我可以理解为什么它不起作用,但是我似乎无法就我的问题获得正确的逻辑。 Could I make it a nested foreach using the same datalist and loop through each row again? 我可以使用相同的数据列表使其成为嵌套的foreach,然后再次遍历每一行吗? Or is that not effective in what I am trying to achieve. 还是这对我想要达到的目标没有效果。

If anymore info is needed I can add it. 如果需要更多信息,我可以添加它。

What you could do instead is first grab the DataListItem that was clicked, and find its TextBox: 相反,您可以做的是首先获取被单击的DataListItem ,然后找到其TextBox:

LinkButton clickedButton = (LinkButton)sender;
DataListItem clickedItem = (DataListItem)clickedButton.NamingContainer;
TextBox clickedTextbox = (TextBox)clickedItem.FindControl("txtCardCode");

Then after that, iterate through the DataList's items, comparing the one you just clicked with the rest of them: 然后,遍历DataList的项目,将您刚刚单击的项目与其余项目进行比较:

foreach (DataListItem dli in dlGiftCode.Items)
{
    if (dli != clickedItem)
    {
        TextBox tb = (TextBox)dli.FindControl("txtCardCode");
        if (tb.Text == clickedTextbox.Text)
        {
            giftCount++;            
        }
    }
}

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

相关问题 比较 XML 文件是否相等的最佳方法是什么? - What is the best way to compare XML files for equality? 比较位图列表中图像的最佳方法是什么 - What is the best way to compare images in a bitmap list 比较两个不同枚举的最佳方法是什么? - What is the best way to compare two different enums? 比较 Double 和 Int 的最佳方法是什么? - What's the best way to compare Double and Int? 在C#中,将字符串与null进行比较以及“”返回true的最佳方法是什么 - In C#, what is the best way to compare strings with null and “” return true 比较 c# 中两个对象的最佳方法是什么 - What is the best way to compare two objects in c# 在 asp net core 中比较日期与 MongoDb 的最佳方法是什么 - what is the best Way to compare dates with MongoDb in asp net core 比较日期和可能不是日期的字符串的最佳方法是什么? - What is the best way to compare a date and string that may not be a date? 比较C#中忽略大小写的2个字符的最佳方法是什么? - What is the best way to compare 2 characters ignoring case in C#? 在C#中比较复杂对象的两个列表的最佳方法是什么 - What is the best way to compare two list of complex object in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM