2010-02-04 87 views
0

獲取FormView中的控件的訣竅是什麼?我使用FindControl()獲取它們,但現在我無法訪問它們。例如:我在FooterTemplate上有一些ImageButton,當它涉及到FormView中的控件時,我可以很順利地獲得這些圖像!零控制。你認爲我應該在每個模板中對它們進行不同的命名嗎? 這讓我想起造成這種噪音的桌子!在ASP.NET FormView中填充控件的表,獲取控件?

我正在使用DataBound事件並檢查特定模式!有任何想法嗎?謝謝。

[增訂]

這是工作

  if (this.kataSistimataFormView.CurrentMode == FormViewMode.Edit) 
     { 
      ImageButton update = (ImageButton)this.kataSistimataFormView.FindControl("btnUpdate"); 
      update.Visible = true; 

但這種出於某種原因沒有

 CheckBox chkBoxPaidoi = kataSistimataFormView.FindControl("chkBoxPaidoi") as CheckBox; 
+0

所以你正在使用的FindControl就好了...然後突然就停止工作:如果你知道ID,您可以使用此遞歸函數:

private Control FindControlRecursive(Control root, string id) { if (root.ID == id) { return root; } foreach (Control c in root.Controls) { Control t = FindControlRecursive(c, id); if (t != null) { return t; } } return null; } 

發現這裏?驚人!在這裏使用FindControl。 – Bryan 2010-02-04 23:40:52

+0

仍然ImageButton工作,它只是在表內的其他控件上返回null。 – 2010-02-04 23:52:19

回答

0

FindControl已不是遞歸。我的意思是,它只會找到控件的子控件中的控件,它們不會搜索子控件的任何子控件。

如果您已將控件放置在您之前尋找的控件中另一個控件,那麼你將不得不在新的控件中搜索,或者如果你仍然想使用kataSistimataFormView作爲父控件,你可能不得不使用遞歸搜索。

谷歌對於「findcontrol遞歸」,有一些很好的例子,你可能只是剪切和粘貼。

+0

我猜這意味着我懷疑,因爲他們現在進入Html錶行和列我必須得到表,然後搜索它,這如果它不是一些噪音:) – 2010-02-05 00:13:56

0

看起來這是由於各種模板上的相同命名標識引起的,即Insert,Edit,Item。即使這是由編譯器支持的,當你以後需要編程時會遇到問題。

謝謝大家。