用于循环,用于表上的单选按钮

本文关键字:用于 单选按钮 循环 | 更新日期: 2023-09-27 18:28:39

嘿,伙计们,我的网上有这些代码:

<asp:Panel ID="Panel1" runat="server"><table>
      <tr>
           <td>
              <asp:RadioButton ID="rdbParametroExterno" Text="[P] Parametro Externo" runat="server"
                                                    Enabled="False" /><br />
              <asp:RadioButton ID="rdbDatoGeneral" Text="[G] Dato General" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFormula" Text="[F] Formula" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFormulaAcumulable" Text="[A] Formula Acumulable" runat="server"
                                       Enabled="False" /><br />
            <asp:RadioButton ID="rdbDatoFijo" Text="[D] Dato Fijo" runat="server" Enabled="False" /><br />
            <asp:RadioButton ID="rdbDatoVariable" Text="[V] Dato Variable" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFuncionInterna" Text="[I] Funcion Interna" runat="server"
                                                    Enabled="False" /><br />
              <asp:RadioButton ID="rdbTablaSistema" Text="[T] Tabla del Sistema" runat="server"
                                                    Enabled="False" /><br />
           </td>
       </tr>
</table></Panel>

我需要循环浏览所有这些,这样当我点击一个按钮时,我就可以使它们成为真的,我正在用前臂尝试,但它似乎不起作用,如果你能帮助我,那就太酷了,谢谢!

protected void btnNew_Click(object sender, ImageClickEventArgs e)
{    
foreach (Control c in Panel1.Controls)
    {
        if (c is RadioButton)
        {
            if (((RadioButton)c).Enabled == false)
            {
                ((RadioButton)c).Enabled = true;
            }
        }
    }
 }

我真的很感谢你的帮助!

用于循环,用于表上的单选按钮

我假设您将所有单选按钮和按钮存储在一个窗体中,该窗体也在Panel中。不幸的是,面板中有更多的子控件,而不仅仅是单选按钮。试试这样的东西:

ASP.NET

    protected void btnNew_Click(object sender, EventArgs e) 
    {
        EnableAllRadioSubControls(Panel1);
    }
    protected void EnableAllRadioSubControls(Control ccontrol)
    {
        foreach (Control c in ccontrol.Controls)
        {
            if(c.Controls.Count > 0)
            {
                EnableAllRadioSubControls(c);
            }else{
                if (c is RadioButton)
                {
                    if (((RadioButton)c).Enabled == false)
                    {
                        ((RadioButton)c).Enabled = true;
                    }
                }
            }
        }
    }

C#

<asp:Panel ID="Panel1" runat="server">
<form id="Form1" runat="server">
<table>
      <tr>
           <td>
              <asp:RadioButton ID="rdbParametroExterno" Text="[P] Parametro Externo" runat="server"
                                                    Enabled="False" /><br />
              <asp:RadioButton ID="rdbDatoGeneral" Text="[G] Dato General" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFormula" Text="[F] Formula" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFormulaAcumulable" Text="[A] Formula Acumulable" runat="server"
                                       Enabled="False" /><br />
            <asp:RadioButton ID="rdbDatoFijo" Text="[D] Dato Fijo" runat="server" Enabled="False" /><br />
            <asp:RadioButton ID="rdbDatoVariable" Text="[V] Dato Variable" runat="server" Enabled="False" /><br />
              <asp:RadioButton ID="rdbFuncionInterna" Text="[I] Funcion Interna" runat="server"
                                                    Enabled="False" /><br />
              <asp:RadioButton ID="rdbTablaSistema" Text="[T] Tabla del Sistema" runat="server"
                                                    Enabled="False" /><br />
           </td>
       </tr>
</table>
<asp:Button ID="btnNew" Text="Clickme" runat="server" onclick="btnNew_Click" />
</form>
</asp:Panel>

这将遍历所有控件,直到找到单选按钮子控件并启用它们。:)