62,046
社区成员
发帖
与我相关
我的任务
分享
Protected Sub btnDisplayValues_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnDisplayValues.Click
ltlValues.Text = ""
For Each c As Control In ph1.Controls
'Find the specific user control that we added to this
'placeholder, and then get the selected values
'for the dropdownlist, checkbox, and textbox and print them to the screen.
If c.GetType.Name.ToLower = "webusercontrol_ascx" Then
Dim uc As UserControl = CType(c, UserControl) '这里转换成自定义的类型,然后就能访问其属性和方法了。
Dim tbx1 As TextBox = uc.FindControl("tbx1")
Dim ddl1 As DropDownList = uc.FindControl("ddl1")
Dim cbx1 As CheckBoxList = uc.FindControl("cbx1")
Dim sb As New System.Text.StringBuilder
sb.Append("Textbox value: " & tbx1.Text & "<br />")
sb.Append("Dropdown value: " & ddl1.SelectedValue & "<br />")
sb.AppendLine("Checkbox values: ")
For Each li As ListItem In cbx1.Items
If li.Selected = True Then
sb.Append(li.Value & "<br />")
End If
Next
sb.Append("<hr />")
ltlValues.Text &= sb.ToString
End If
Next
End Sub
完整代码及解释参考:
Dynamically add and remove user controls
http://www.codeproject.com/Articles/26589/Dynamically-add-and-remove-user-controls