按名称模式迭代引用控件 (VB.NET)
有没有一种方法可以根据名称模式迭代引用 ASP.NET 控件?也许通过“指针”或其他什么?
我拥有的是一大组组合框(每个组合框都有一个关联的 DropDownList 和 TextBox 控件)。 DropDownList 始终默认选择一个项目,但用户可能希望提交 NULL 值。我想出了以下代码来处理三种情况:NULL、现有项目、新项目。
'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 上的存储过程以插入到数据库中。
Is there a way to iteratively reference an ASP.NET control based on it's name pattern? Perhaps by a "pointer" or something?
What I have are a large set of comboboxes (each with an associated DropDownList and TextBox control). The DropDownList always has an item selected by default, but the use may wish to submit a NULL value. I come up with the following code to handle three cases: 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
The problem is, with so many comboboxes, I could easily end up with hundreds of lines of code. I wish to handle this in a more dynamic way, but I do not know if it is possible to reference controls in this way. Say I wanted to reference all the TextBox Controls and DropDownList Controls, respectively.
I can do string formatting with a given naming pattern to generate a name ID for any of the controls because they are all named with the same pattern. For example by attaching a specific suffix, say "_tb" for TextBox Controls and "_ddl" for DropDownList Controls:
Left(item.SelectedItem.ToString, item.SelectedItem.ToString.Length - 3) + "_tb"
Can this sort of thing be done in VB? Ultimately, my goal is to take the value entered/selected by the user, if any, and send it to a stored procedure on SQL Server for insertion into the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。在具有文本框和下拉列表参数的函数内编写代码,然后根据这两个参数编写函数。然后,您只需为每个集合调用该函数,而不是到处复制/粘贴代码。
不要尝试通过名称来选择事物。这是脆弱的,并且对为人类消费而设计的领域提出了机器要求。
Yes. Write your code inside of a function having parameters for the textbox and the dropdown, and then write the function in terms of those two parameters. Then you simply call that function for every set instead of copy/pasting code everywhere.
Don't attempt choosing things by name. That's fragile and imposes machine requirements on a field that'd designed for human consumption.
我不确定适用于 VBA 的相同内容是否适用于您的情况,但我遇到了同样的问题并且能够使用:
Me.MultiPage1.Pages(1).Controls("ref" & i + 1).Value
其中控件包含用户窗体(“Me”)上的所有控件。因此,如果程序中的控件符合连续的命名结构,如果上述内容适用于 vb.net,则很容易处理
I am not sure if the samething that applies VBA will apply to your situation, but I had the same issue and was able to use:
Me.MultiPage1.Pages(1).Controls("ref" & i + 1).Value
where controls encompasses all controls on the userform ("Me"). So if the controls in your program conform to a consecutive naming structure it is easy handle if the above applies vb.net