2007/05/07

運用遞迴深度列舉動態新增控制項

********************
//C#
public List EnumControls(List ctlContainer, Control BaseControl)
{
if (ctlContainer == null)
ctlContainer = new List();
ctlContainer.Add(BaseControl);
foreach (Control ctl in BaseControl.Controls)
ctlContainer = EnumControls(ctlContainer, ctl);
return ctlContainer;
}

'Visual Basic
Public Function EnumControls(ctlContainer As List(Of Control), BaseControl As Control) As List(Of Control)
If ctlContainer Is Nothing Then
ctlContainer = new List(Of Control)()
End If
ctlContainer.Add(BaseControl)
For Each ctl As Control In BaseControl.Controls
ctlContainer = EnumControls(ctlContainer, ctl)
Next ctl
Return ctlContainer;
End Function

********************

參考:微軟MSDN

標籤:

運用遞迴深度搜尋動態新增控制項

********************
// C#
public Control DepthFindControl(string ControlID, Control BaseControl)
{
Control result = BaseControl.FindControl(ControlID);
if (result == null)
{
foreach (Control ctl in Page.Controls)
{
if (ctl.Controls.Count > 0)
result = DepthFindControl(ControlID, ctl);
}
}
return result;
}

'Visual Basic
Public Function DepthFindControl(ControlID As String, BaseControl As Control) As Control
Dim result As Control = BaseControl.FindControl(ControlID);
If result Is Nothing Then
For Each ctl As Control In Page.Controls
If ctl.Controls.Count > 0 Then
result = DepthFindControl(ControlID, ctl)
End If
Next ctl
End If
Return result
End Function

********************


參考: 微軟MSDN

標籤:

動態新增控制項 四步驟

1.控制項建立(Control Creation),在頁面處理時,產生控制項。
2.控制項命名(Control Naming)與套用事件處理常式。
3.控制項資料存取(Control Access)。
4.控制項資料與狀態保存(Control State Persistent)。




標籤: ,