簡體   English   中英

在ASP.Net C#中,在按鈕單擊事件上,我在DataList中找到FileUpload控件,並且顯示錯誤?

[英]In ASP.Net C#, on button click event I find the FileUpload Control within DataList and It gives error?

這是錯誤:

  • 集合已修改; 枚舉操作可能無法執行。

對於每個過程嘗試查找項目時,都會發現此錯誤。

 protected void SubmitButton_Click(object sender, EventArgs e)
 {
      foreach (DataListItem item in this.ImageRepeater.Items)
      {
           FileUpload fup = (FileUpload)ImageRepeater.FindControl("ImageUpload");
           if (fup.HasFile)
           {
               updateImageChanges();
               divTopImageCheckChangedmessage.Visible = false;
            }
      }         
   }

我的要求是我要滿足檢查是否沒有文件加載到ASP.Net DataList中的Fileupload控件中,然后不允許updateImageChanges(); 被擊中的功能。

我將感謝你們。

由於updateImageChanges()不帶參數,因此我假設它再次遍歷您的轉發器項目? 它如何知道要使用哪個項目? 您在該函數中所做的任何操作(因為您沒有為其發布代碼)都是錯誤的根源,而不是您所發布的代碼中的錯誤。 但是,此解決方案將為您工作:

您可以更新該函數以采用參數(例如FileUpload控件),然后將其從if語句傳遞給updateImageChanges()方法。 這樣,該方法將僅更新更改,而無需再次遍歷轉發器項以獲取所需的內容。

像這樣:

  protected void SubmitButton_Click(object sender, EventArgs e)
  {
      foreach (DataListItem item in this.ImageRepeater.Items)
      {
           FileUpload fup = (FileUpload)ImageRepeater.FindControl("ImageUpload");
           if (fup.HasFile)
           {
               updateImageChanges(fup);
               divTopImageCheckChangedmessage.Visible = false;
            }
      }         
   }

   private void updateImageChanges(FileUpload fup)
   {
       // remove your code that loops through to get to the correct file upload
       // leave code that works with current fup and use passed parameter instead of the repeater items collection
   }

不知道為什么要遍歷DataList項目,我認為您的代碼可能看起來像這樣:

檢查此示例代碼以獲取

protected void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    DataListItem di = btn.NamingContainer as DataListItem;
    FileUpload fu = di.FindControl("fu") as FileUpload;       
    if (fu.HasFile)
    {
     // save to the database :


    }


}

我認為此錯誤是您的updateImageData調用方法提供的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM