none
percorrer todos textbox da pagina e limpar RRS feed

  • Pergunta

  • bom dia ,pessoal tenho o methodo abaixo que montei para limpar todos campos do meu form mas não esta funcionando ,acredito que não funciona porque todos text estão dentro de um Panel como faço percorrer o panel e pegar os controles textbox. este Panel eu chamo com modalextender ajax.

    protected void Limpa_Campos()
        {

            foreach (Control c in Page.Controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Text = "";
                }
            }

        }

    Codigo aspx com texts

      <asp:Panel ID="Panel1" runat="server" Style="display: none;" CssClass="ModalWindow">
                <div class="DivFrame_Paginas">
                    <center>
                        <p style="background-color: #316BA5; color: Yellow; font-size: 14px; text-align: center">
                            CADASTRO DE EMPRESAS
                        </p>
                    </center>
                    <fieldset style="text-align: left; width: 560px; height: 285px; background-color: #FFF;
                        color: #000">
                        <legend style="text-align: left; border: 1px solid #000000; background-color: #CCC;
                            font-size: small;"><span>Cadastrar Dados Empresa</span> </legend>
                        <br />
                        <table cellpadding="1" cellspacing="1" border="0" width="100%" style="background-color: #FFF;
                            color: #000">
                            <tr>
                                <td>
                                    Grupo:
                                </td>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server" Height="18px"></asp:TextBox>
                                    <%--    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Campo Grupo Obrigatorio"
                                        Text="*" ControlToValidate="txtgrupo"></asp:RequiredFieldValidator>
                                    <asp:ValidatorCalloutExtender ID="Validatorcalloutextender5" runat="server" TargetControlID="RequiredFieldValidator1"
                                        Width="270px" PopupPosition="TopLeft">
                                    </asp:ValidatorCalloutExtender>--%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Matriz:
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Nome:
                                </td>
                                <td>
                                    <asp:TextBox ID="txtnome" runat="server" Width="310px" Height="18px"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    CNPJ:
                                </td>
                                <td>
                                    <asp:TextBox ID="txtcnpj" runat="server" Height="16px" Width="120px"></asp:TextBox>
                                    <asp:MaskedEditExtender ID="txtcnpj_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
                                        CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
                                        CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
                                        Enabled="True" Mask="99,999,999/9999-99" TargetControlID="txtcnpj" MaskType="None">
                                    </asp:MaskedEditExtender>
                                    <%--    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Campo CNPJ Obrigatorio"
                                        Text="*" ControlToValidate="txtcnpj"></asp:RequiredFieldValidator>
                                    <asp:ValidatorCalloutExtender ID="Validatorcalloutextender1" runat="server" TargetControlID="RequiredFieldValidator3"
                                        Width="270px" PopupPosition="TopLeft">
                                    </asp:ValidatorCalloutExtender>--%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Razão Social:
                                </td>
                                <td>
                                    <asp:TextBox ID="txtrazao" runat="server" Height="18px" Width="145px"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    IE:
                                </td>
                                <td>
                                    <asp:TextBox ID="txtie" runat="server" Height="18px" Width="86px"></asp:TextBox>
                                    <asp:MaskedEditExtender ID="txtie_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
                                        CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
                                        CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
                                        Enabled="True" Mask="999/9999999" TargetControlID="txtie" MaskType="Number">
                                    </asp:MaskedEditExtender>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Data Fundação:
                                </td>
                                <td class="style158">
                                    <asp:TextBox ID="txtdata" runat="server" Height="18px" Width="87px"></asp:TextBox>
                                     <asp:CalendarExtender ID="CalendarExtender1" Format="dd/MM/yyyy" PopupPosition="BottomLeft" runat="server"  TargetControlID="txtdata"></asp:CalendarExtender>
                                    <asp:MaskedEditExtender ID="txtdata_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
                                        CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
                                        CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
                                        Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txtdata" UserDateFormat="DayMonthYear">
                                    </asp:MaskedEditExtender>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Ramo&nbsp; Atividade:
                                </td>
                                <td>
                                    <asp:DropDownList ID="ddlramo" runat="server" Height="20px" Width="222px">
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="left">
                                    <b>
                                        <asp:Label ID="lblresult" runat="server"></asp:Label></b>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="left">
                                    <asp:Image runat="server" ImageUrl="~/images/msg-ok.gif" Visible="false" ID="image1" />
                                    <asp:Image ID="Image2" runat="server" ImageUrl="~/images/msg-error.gif" Visible="false" />
                                </td>
                            </tr>
                        </table>
                    </fieldset>
                    <p style="text-align: left;">
                        <asp:Button ID="OkButton" runat="server" Text="Gravar" OnClick="OkButton_click" />
                        <asp:Button ID="CancelButton" runat="server" Text="Cancel" />
                    </p>
                </div>
            </asp:Panel>

                 
    sexta-feira, 30 de março de 2012 11:26

Respostas

  • Amigo,

    Utilize recursividade.

    Tente isso:

    protected void Limpa_Campos(Control control)
    {
          foreach (Control c in control.Controls)
          {
             if (c is TextBox)
             {
                (c as TextBox).Text = "";
             }
    
             if(c.HasControls)
                Limpa_Campos(c);
          }
    }


    Carlos Eduardo Ferreira

    sexta-feira, 30 de março de 2012 11:37

Todas as Respostas

  • tenta isso

    private void LimparCampos()
            {
                txtentidade.Text = "";
                txtnomeentidade.Text = "";
                txtresponsavel.Text = "";
                txtemail.Text = "";
                txtfone.Text = "";
                txtdtimplantacao.Text = "";
                txtdttreinamento.Text = "";
                txtendereco.Text = "";
                txtsequencial.Text = "";
                txtCodescola.Text = "";
                txtBairro.Text = "";
                txtpatrimonio.Text = "";
                txtserievalidador.Text = "";
                txtcep.Text = "";
                txtobs.Text = "";
                txtdtconclusao.Text = "";
            }


    Junior

    sexta-feira, 30 de março de 2012 11:34
  • Amigo,

    Utilize recursividade.

    Tente isso:

    protected void Limpa_Campos(Control control)
    {
          foreach (Control c in control.Controls)
          {
             if (c is TextBox)
             {
                (c as TextBox).Text = "";
             }
    
             if(c.HasControls)
                Limpa_Campos(c);
          }
    }


    Carlos Eduardo Ferreira

    sexta-feira, 30 de março de 2012 11:37
  • cara este codigo funcionou Bleza grato
    sexta-feira, 30 de março de 2012 11:52