2014-10-18 30 views
1

我有一個asp頁面的主頁和ContentPlaceHolder 我想找到所有控件(複選框,文本框,標籤,...)在我的頁面 我這樣做(沒有masterpage )正常獲取控件在ContentPlaceHolder asp.net與每個

Dim c As Control 
    For Each c In Page.Controls 
     For Each childc In c.Controls 
       If CType(childc, TextBox).Text <> "" Then 

但是這個代碼不與母版 回答我不怎麼一回事,因爲我希望所有的控件,需要此代碼

TextBox TB=Master.FindControl("ContentPlaceHolder1").FindControl("textbox1") as TextBox; 

+0

您可以從FindControl(「ContentPlaceHolder1」)。Controls'開始,而不是'Page.Controls'開始。但是這不適用於嵌套控件(比如在「Panel」或者「GridView」中)。你爲什麼需要它。在正確的位置搜索,而不是循環所有子控件。 – 2014-10-18 21:33:46

+0

謝謝我有約60控制(文本框,DRP,CHK,...)和數據庫我想要的時候,用戶點擊發送按鈕從這個for-loop搜索所有控制和保存數據庫中的值我做的沒有正確的masterpage,但我不能做現在我測試你的解決方案和chane page.control,但視覺工作室說錯誤「對象引用未設置爲對象的實例」 – user1670642 2014-10-19 05:07:59

回答

2

你可以這樣做。遍歷所有控件,根據您的情況在循環迭代中檢查它們的Type,採取行動或獲取價值並對其進行操作。

For Each c In Master.FindControl("ContentPlaceHolder1").Controls 
    If TypeOf c Is TextBox Then 
     Dim text As TextBox = CType(c, TextBox) 
     ' Write code textbox handling 
    End If 
    If TypeOf c Is CheckBox Then 
     Dim chk As CheckBox = CType(c, CheckBox) 
     ' Write code checkbox handling 
    End If 
    If TypeOf c Is DropDownList Then 
     Dim ddl As DropDownList = CType(c, DropDownList) 
     ' Write code Dropdownlist handling 
    End If 
Next 
+1

感謝每一個機構 – user1670642 2014-10-19 08:18:02

相關問題