简体   繁体   中英

How to find the checked radio button in a subset container?

I have a list of radio button, not radiobuttonlist, I need to find out which one is checked. The container is divs within a known wizardstep. Since only the wizardstep id is known, how to find radio button within divs under it? The structure is something like this:

<asp:WizardStep runat="server" ID="TypeStep" StepType="Step" Title="Business Type">
   <div id="A" runat="server">
      <asp:radiobutton .....

   </div>
   <div id="B" runat="server>
      <asp:radiobutton .....

   </div>
    ...
</asp:WizardStep>

My code is as follows:

foreach (RadioButton rb in TypeStep.Controls.OfType<RadioButton>()){
  ...
}

But it can not find any radiobutton, unless I change the TypeStep to "A" div, but in my code, which div is not knew. I can only use TypeStep to search checked radiobutton.

Any idea?

So Controls Property only returns one level of child controls. I suggest you crete an extension that will recursively search for the controls. I suggest also you pass in a maximum depth. This would be the more generic way. However, in your example you need another nested for loop. Code here:

foreach (Control c in TypeStep.Controls)
{
    foreach (RadioButton rb in c.Controls.OfType<RadioButton>())
    { 
          ... 
    }     
}

This will search another one level down. As stated previously, the Controls property returns one level at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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