for循环,用于桌子上的单选按钮

时间:2012-03-21 21:59:39

标签: c# asp.net

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

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

我需要循环遍历所有这些,所以当我点击一个按钮时,我可以启用它们,我正在尝试使用foreach,但它似乎无法工作,如果你能帮助我那很酷,谢谢!

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

    }
 }

我非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我假设您将所有单选按钮和按钮存储在表单内,该表单也在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>

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