开发者

How can I store the actual control in a list?

Long sto开发者_运维问答ry 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!


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).


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); 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜