【发布时间】:2011-11-25 11:47:12
【问题描述】:
有没有一种方法可以根据其名称模式迭代引用 ASP.NET 控件?也许通过“指针”或其他什么?
我拥有的是一大组组合框(每个组合框都有一个关联的 DropDownList 和 TextBox 控件)。 DropDownList 始终默认选择一个项目,但用户可能希望提交一个 NULL 值。我想出了以下代码来处理三种情况:NULL、Existing Item、New Item。
'TextBoxControl & DropDownListControl should iteratively reference their
' respective TextBox & DropDownList controls by the actual control name
If StrComp(TextBoxControl.Text, DropDownListControl.SelectedItem.ToString) = 0 Then
'When an Existing Item is selected, Do Something
ElseIf Not TextBoxControl.Text = "" Then
'When a New Item is entered, Validate & Do Something
Else
'When NULL, Do Something
End If
问题是,有这么多组合框,我很容易得到数百行代码。我希望以更动态的方式处理这个问题,但我不知道是否可以以这种方式引用控件。假设我想分别引用所有的 TextBox 控件和 DropDownList 控件。
我可以使用给定的命名模式进行字符串格式化,以便为任何控件生成名称 ID,因为它们都以相同的模式命名。例如,通过附加特定的后缀,对 TextBox 控件说“_tb”,对 DropDownList 控件说“_ddl”:
Left(item.SelectedItem.ToString, item.SelectedItem.ToString.Length - 3) + "_tb"
这种事情可以在VB中完成吗? 最终,我的目标是获取用户输入/选择的值(如果有),并将其发送到 SQL Server 上的存储过程以插入数据库。
【问题讨论】:
标签: asp.net vb.net dynamic controls iteration