[英]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.