2011-04-11 67 views
0

在我創建基於數據編寫的用戶控件的新實例的頁面上工作。我使用構造函數注入將過濾的數據傳遞給用戶控件。但是,當用戶控制渲染時,僅爲所有用戶控件渲染注入的最後一組數據。看來我引用的是同一個實例,而不是創建一個新的獨立實例。想法?注入不同數據的多個用戶控件顯示相同的數據

protected void Page_Init(object sender, EventArgs e) 
     { 
      var data = <my data comes from here>; 
      var yearsInData = data.OrderByDescending(x=>x.Year).Select(x => x.Year).Distinct(); 

      foreach(var year in yearsInData) 
      { 
       var employers = data.OrderBy(x=>x.EmployerName).Where(x => x.Year == year).Select(x => x.EmployerName).Distinct(); 
       foreach (var employer in employers) 
       { 
        var eob = data.Where(x => x.Year == year).Where(x => x.EmployerName == employer); 
        if (eob.Count() > 0) 
        { 
         var ctl = LoadControl(@"~\Shared\Controls\AnnualEOBControl.ascx", eob); 
         pnlMain.Controls.Add(ctl); 
        } 
       } 
      } 
     } 

這裏是LoadControl方法:

private UserControl LoadControl(string userControlPath, params object[] constructorParameters) 
     { 
      var constParamTypes = new List<Type>(); 
      foreach (var constParam in constructorParameters) 
      { 
       constParamTypes.Add(constParam.GetType()); 
      } 

      var ctl = Page.LoadControl(userControlPath) as UserControl; 

      // Find the relevant constructor 
      if (ctl != null) 
      { 
       var constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()); 

       // And then call the relevant constructor 
       if (constructor == null) 
       { 
        throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType); 
       } 
       constructor.Invoke(ctl, constructorParameters); 
      } 

      // Finally return the fully initialized UC 
      return ctl; 
     } 

回答

0

它聞起來像一個封閉的問題。我的預感是eob只是在每次迭代期間重新分配一個值,而不是重新創建。由於您的控件都會引用eob而不是eob返回的數據,因此它們在渲染時都會使用相同的值。

你的LoadControl重載是什麼樣的?我仔細閱讀了你引用的文章,但我想看看你做了什麼,具體來說。我想知道是否簡單地將eob中的值重新賦值給在LoadControl方法中聲明的變量會讓你超越駝峯。強制控件使用更嚴格範圍的變量,以便他們無法看到對方的參數。

編輯:發現了題目的,從而可以參照:What are 'closures' in .NET?

給這個一掄:

private UserControl LoadControl(string userControlPath, params object[] constructorParameters) 
    { 
     var constParamTypes = new List<Type>(); 
     foreach (var constParam in constructorParameters) 
     { 
      constParamTypes.Add(constParam.GetType()); 
     } 

     var ctl = Page.LoadControl(userControlPath) as UserControl; 

     // Find the relevant constructor 
     if (ctl != null) 
     { 
      var constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()); 

      // And then call the relevant constructor 
      if (constructor == null) 
      { 
       throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType); 
      } 

      // constructor.Invoke(ctl, constructorParameters); 
      object[] cp = constructorParameters; 
      constructor.Invoke(ctl, cp); 
     } 

     // Finally return the fully initialized UC 
     return ctl; 
    } 
+0

它發生在我看來,它可能是一個更清潔,只是調用.ToArray()當你餵養LoadControl。同樣的效果。 – 2011-04-11 18:00:16

+0

得到了同樣的結果。還有什麼我需要嘗試? – 2011-04-11 18:42:30

+0

嘗試使用.ToArray()方法。我承認不是一名封閉專家,但我不能動搖這種情況。 – 2011-04-11 18:46:01

0

如果我有這個問題,有一個基類,我可以儀器和方法來生產某種沒有太多的麻煩痕跡,我

  • 把一個const實例成員GUID和日期時間,無論是在構造函數初始化它
  • 放幾毫秒單獨調用之間睡覺的構造函數,因此時間戳

我只是在想,可能會給一個提示,這是怎麼回事,並導致它的根。

+0

是否做到了。我在構造函數中傳遞了DateTime.Now.Ticks,並做了一個Response.Write。所有獨特的條目。 EOB數據也是獨一無二的。然而,我用來顯示的網格只呈現發送它的最後一組數據。我甚至將網格數據源設置爲立即傳入的數據。嗯 – 2011-04-11 18:52:58

+0

我們可以看到綁定代碼嗎? – 2011-04-11 18:57:10

+0

也許你標記了一個靜態字段,並不意味着什麼?或者這可能是一些奇怪的LINQ的東西? (tack .ToList()結束...) - 我不在想法...祝你好運:) – 2011-04-11 19:05:47

相關問題