Iterate though all controls on asp.net page

前端 未结 3 1819
误落风尘
误落风尘 2021-01-15 05:14

I\'m using ascx and I need to iterate through all of the controls and selects each that have cssClass attribute set to \'required\'.

I have the following code:

3条回答
  •  既然无缘
    2021-01-15 05:53

    The Control class doesn't have that CssClass property, the WebControl does. So try to cast your childControl to WebControl. If that worked, then you can access the CssClass property.

    WebControl webCtrl = childControl as WebControl;
    if (webCtrl != null)
    {
       webCtrl.CssClass = "test";
    }
    

提交回复
热议问题