Code to clear all text boxes in C#.Net/ASP.Net

By
Advertisement
hi

<div style="width: 100%;">
<form id="form1" runat="server">
<div style="width: 300px; float: left;">
<asp:Panel ID="pnlTable1" runat="server">
<p>
<strong>Panel 1 Controls</strong></p>
<div style="margin: 40px auto;">
<table>
    <tr>
        <td>
            Name1
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            City1
        </td>
        <td>
            <asp:TextBox ID="txtCity" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            Location1
        </td>
        <td>
            <asp:TextBox ID="txtLocation" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            Mobile1
        </td>
        <td>
            <asp:TextBox ID="txtMobile" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="btnReset" runat="server" Text="Clear Panel 1 Controls" OnClick="btnClear1_Click" />
        </td>
    </tr>
</table>
</div>
</asp:Panel>
</div>
<div style="width: 300px; float: left;">
<asp:Panel ID="pnlTable2" runat="server">
<p>
<strong>Panel 2 Controls</strong></p>
<div style="margin: 40px auto;">
<table>
    <tr>
        <td>
            Name2
        </td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            City2
        </td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            Location2
        </td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
            Mobile2
        </td>
        <td>
            <asp:TextBox ID="TextBox4" runat="server" Text="" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="btnClear2" runat="server" Text="Clear Panel 2 Controls" OnClick="btnClear2_Click" />
        </td>
    </tr>
</table>
</div>
</asp:Panel>
</div>
<div style="clear: both;">
<asp:Button ID="btnClearAll" runat="server" Text="Clear All" OnClick="btnClearAll_Click"
Style="color: #F8F5F5; background-color: #0606F7; text-align: center; margin-left: 200px;" />
</div>
<div style="clear: both; color: red;">
<asp:Literal ID="litMsg" runat="server" Text="" />
</div>
</form>

</div>

hi


private void ClearAllContols(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        if (ctrl is DropDownList)
            ((DropDownList)ctrl).SelectedIndex = 0;
        ClearAllContols(ctrl.Controls);
    }
}
protected void btnClear1_Click(object sender, EventArgs e)
{
    ClearAllContols(pnlTable1.Controls);
    litMsg.Text = "Panel 1 Controls alone cleared.";
}
protected void btnClear2_Click(object sender, EventArgs e)
{
    ClearAllContols(pnlTable2.Controls);
    litMsg.Text = "Panel 2 Controls alone cleared.";
}
protected void btnClearAll_Click(object sender, EventArgs e)
{
    ClearAllContols(Page.Controls);
    litMsg.Text = "Cleared all the Controls in the page.";

}

0 comments:

Post a Comment

Online Casino