[英]What is the best way to compare rows in a datalist
我要問的一般問題是比較數據列表中行的最佳方法是什么。 我擁有的代碼是關於一個數據列表的,該數據列表包含一個文本框和兩個按鈕,一個提交,一個清除。 當用戶單擊提交時,我正在嘗試將文本框與其他行進行比較。 按鈕單擊是命令,我將ItemIndex作為命令參數傳遞,因此我確實知道按鈕單擊發生在哪一行。 我正在使用foreach循環遍歷每一行。
以下代碼是我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
}
}
for循環是我無法找到重復項的嘗試。 我可以理解為什么它不起作用,但是我似乎無法就我的問題獲得正確的邏輯。 我可以使用相同的數據列表使其成為嵌套的foreach,然后再次遍歷每一行嗎? 還是這對我想要達到的目標沒有效果。
如果需要更多信息,我可以添加它。
相反,您可以做的是首先獲取被單擊的DataListItem
,然后找到其TextBox:
LinkButton clickedButton = (LinkButton)sender;
DataListItem clickedItem = (DataListItem)clickedButton.NamingContainer;
TextBox clickedTextbox = (TextBox)clickedItem.FindControl("txtCardCode");
然后,遍歷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.