Making an indexed control array?

后端 未结 7 1653
失恋的感觉
失恋的感觉 2020-12-03 16:15

Has C# indexed control arrays or not? I would like to put a \"button array\" for example with 5 buttons which use just one event handler which handles the index of all this

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 16:38

    If you are working with Web Forms and not MVC, you can acces a collection of controls on the page as shown in Using the Controls Collection in an ASP.NET Web Page. Essentially the controls collection is a tree with the page hosting the first level of child controls and some items having children of their own. See How to: Locate the Web Forms Controls on a Page by Walking the Controls Collection for an example of how to follow the tree.

    Also, see How to: Add Controls to an ASP.NET Web Page Programmatically.

    You can use the same event handler for multiple items as long as the signature required is the same.

    For Windows Forms this is nearly identical since they're based on similar architectural models, but you'll want Control.Controls Property and How to: Add Controls to Windows Forms.

提交回复
热议问题