将控件添加到集合并从集合更新
我有一个 ASP.NET 应用程序,页面上有很多文本框,需要在程序执行的各个点进行更新。
这些文本框实际上属于某个类,因此为了方便更新,我想我可以创建一个字典(字符串,对象)并将 control.ID
和 control
添加到然后进行更新,执行如下操作:(
在更新 textbox.text 的情况下):
for each kv as KeyValuePair(Of string, object) in mytextboxes
if (kv.Key.Contains("textboxid")) then
DirectCast(kv.Value, TextBox).Text = mystring
end if
next
但是,文本框的 text 属性实际上并未更新。我主要是想避免每次更新文本框时都必须手动执行 textbox.text = somestring
。
这是一个可行的解决方案吗? 如果是的话,我做错了什么?
I have an ASP.NET app with lots of textboxes all over the page that need updating at various points through program execution.
These textboxes actually belong to a certain class, so for easy updating I thought I could create a Dictionary(Of string, object) and add the control.ID
and the control
to it and then for updating do something like this:
(in case of updating textbox.text):
for each kv as KeyValuePair(Of string, object) in mytextboxes
if (kv.Key.Contains("textboxid")) then
DirectCast(kv.Value, TextBox).Text = mystring
end if
next
However the text property of the textbox does not actually get updated. I'm mainly trying to avoid having to manually do textbox.text = somestring
for each one of my textboxes every time I have to update them.
Is this a feasible solution that could be made to work?
If so, what have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的函数需要直接转换控件,而不是 KeyValuePair 集合。尝试这样的事情......
然后打电话......
Your function needs to cast the control directly, not a KeyValuePair collection. Try something like this...
Then to call...
如果您必须使用字典,请尝试以下操作:
If you have to use a dictionary, try this:
我有一个使用 100 个按钮的项目,它们共享一个事件处理程序。如果您可以在数组中创建所有文本框,您也许能够定义一个公共事件处理程序。以下片段将用作参考。希望VB代码仍然有帮助。
如果您想测试代码,您需要有一个表单和 10 X 10 的布局。如果有帮助,请告诉我。
I have a project that uses 100 buttons and they share one event handler. If you could create all of the TextBoxes in an Array, you may be able to define a common event handler. The following snippit would be used as a reference. Hope the VB code is still helpful.
If you want to test the code, you need to have a Form and a Layout of 10 X 10. Let me know if it helps.