如何将实际控件存储在列表中?

发布于 2024-09-27 18:54:24 字数 176 浏览 2 评论 0原文

长话短说,我想将动态加载的用户控件粘贴到引用已加载的实际对象的列表中。这样我就可以进入列表并从用户控件中提取结果。我想使用 mycontrol.GetResult() ,然后它将引用该控件并从已填写的表单中获取结果,结果将以字符串形式返回。我不希望它初始化相同类型的新控件,因为那样我将无法收到结果。有什么建议吗?

谢谢!

Long story short, I would like to take my usercontrols that are loaded dynamically and stick them into a list that references the actual object that has been loaded. So I can go into the list and pull results from the usercontrol. I would like to use mycontrol.GetResult() and it will then reference the control and grab the results from the form that has been filled out, which the results will be returned as a string. I do not want it to initialize a new control of the same type because I will not be able to receive my results then. Any suggestions?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰ゝ天使的微笑 2024-10-04 18:54:24

只需创建一个控件列表:

var controls = new List<Control>();

foreach(var control in Page.Controls)
{
    controls.Add(control);
}

然后您可以使用该列表根据需要引用每个控件(这是一个简单的示例......您填充列表的代码很可能会更复杂)。

Just create a list of Controls:

var controls = new List<Control>();

foreach(var control in Page.Controls)
{
    controls.Add(control);
}

You can then use that list to reference each of the controls as needed (this is a simple example...your code to populate the list will most likely be more complicated).

梦醒时光 2024-10-04 18:54:24

谁以编程方式添加控件?页面还是用户控件?该组件可以做到这一点。否则,另一个技巧是用户控件动态加载其他用户控件。您可以为页面提供一个接口定义,例如具有 List 类型的 IDynamicControlHolder,并在动态控件(或其他位置)中执行操作。

if (this.Page is IDynamicControlHolder)
   // change this to the appropriate reference
   ((IDynamicControlHolder)this.Page).DynamicControls.Add(this); 

Who adds the controls programmatically? THe page or a user control? That component could do it. Otherwise, another trick is say a user control dynamically loads other user controls. You could give the page an interface definition say IDynamicControlHolder that has a List type, and do in the dynamic control (or somewhere else).

if (this.Page is IDynamicControlHolder)
   // change this to the appropriate reference
   ((IDynamicControlHolder)this.Page).DynamicControls.Add(this); 
粉红×色少女 2024-10-04 18:54:24

因此,我已经创建了类似的代码并将控件加载到列表中,但我不确定如何从放回对象的表单中获取结果。看到代码示例后,我意识到我已经在这样做了,所以必须有更好的方法来获得我想要的东西。我操纵了现有的方法之一,现在当用户进入验证屏幕时,我使用它将结果加载到我的对象中。这很好用,谢谢你确认我做得对。

So I already had similar code created and the controls loaded into a list but I was not sure how to get the results from the forms placed back into my object. After seeing code examples I realized I was already doing that so there had to be a better way to get what I want. I manipulated one of my existing methods and now I use it to load the results into my object when the user goes to the verification screen. This works great, thank you for confirming for me I was doing it right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文