简体   繁体   English

在ASP.NET中,如何在字典中存储控件

[英]In ASP.NET, how to store controls in a Dictionary

I am storing ASP.NET Controls (Tables, Buttons, TextBoxes) in a Dictionary in a webform application, so that they can be accessed directly from user controls, instead of having to do a recursive search for them from other user controls. 我将ASP.NET控件(表,按钮,文本框)存储在Webform应用程序的“词典”中,以便可以直接从用户控件访问它们,而不必从其他用户控件中进行递归搜索。 When they are added to the dictionary, I can verify that the visible control on the .ascx control is identical to the object in the Dictionary: X == Y returns true. 将它们添加到字典中后,我可以验证.ascx控件上的可见控件是否与字典中的对象相同:X == Y返回true。 But later on, when I want to do something like changing a background color, or disabling a button, X == Y returns false. 但是稍后,当我想做一些更改背景颜色或禁用按钮的操作时,X == Y返回false。 A change that I make to the object in the Dictionary (which is declared as static) is not reflected in the visible GUI control. 我对Dictionary中的对象(声明为静态)所做的更改未反映在可见的GUI控件中。

How do I correct this? 我该如何纠正?

(it's difficult to give you a helpful answer without seeing any code, and without understanding what you're trying to achieve, but ...) (很难找到有用的答案,而无需看任何代码,也不了解您要实现的目标,但是...)

Whenever a Postback is made, a new instance of the page and all its controls is created. 每当进行回发时,都会创建该页面及其所有控件的新实例。 Therefore the control instances stored in your dictionary no longer match with the control instances on the current page instance. 因此,存储在词典中的控件实例不再与当前页面实例上的控件实例匹配。 They are still the instances created when the page was requested for the first time. 它们仍然是首次请求页面时创建的实例。


But in any case, storing control instances in a static dictionary is a very bad idea (eg think about what happens when multiple users call your page in parallel). 但是无论如何,将控制实例存储在静态字典中是一个非常糟糕的主意(例如,考虑多个用户并行调用您的页面时发生的情况)。

Please explain what you want to achieve and why you think your dictionary is necessary. 请说明您要达到的目标以及为什么您认为需要字典。 It will then probably be easier to help you. 这样可能会更容易为您提供帮助。

The dictionary stores information about each control. 词典存储有关每个控件的信息。 However, the controls that the UI displays are separate. 但是,UI显示的控件是分开的。 Every time you make a change to one element in the dictionary, you have to set the actual UI components to = their corresponding dictionary element. 每次对字典中的一个元素进行更改时,都必须将实际的UI组件设置为=它们对应的字典元素。 So 所以

Dictionary["textBoxExampleKey"].Text = "blah";

won't change the actual textbox. 不会更改实际的文本框。 To update the textbox to match the value in the dictionary, do` 要更新文本框以匹配字典中的值,请执行`

textBoxExample.Text = Dictionary["textBoxExampleKey"].Text;

I recommend making a method that updates the entire UI to match the dictionary for the sake of simplicity. 为了简单起见,我建议使用一种方法来更新整个UI以匹配字典。 So this method would be something like 所以这种方法就像

private void UpdateUI()
{
    textBoxExample.Text = Dictionary["textBoxExampleKey"].Text;
    labelExample.Text = Dictionary["labelExampleKey"].Text;
    listBoxExample.DataSource = Dictionary["listBoxExampleKey"].DataSource;
    Refresh();
}

The "Refresh()" method reloads your UI so that all changes are made visible, which could also be part of your problem. “ Refresh()”方法重新加载您的UI,以便所有更改都可见,这也可能是问题的一部分。

TL;DR: Update your actual controls, refresh your UI. TL; DR:更新您的实际控件,刷新您的UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM