Today’s tutorial is a rather simple but very effective trick that will improve the overall user experience in your next C# project. If you are building a site that is form intensive (let’s say an online instruction course website) and the user would like to remove text boxes that he/she has inputed because he/she is not happy with his answer. Instead of the user going through and deleting all of the answers himself, you can create a button and tie an event to it that will clear all Text Box fields for him/her.
Remove Text Boxes using C#
With this code, you can scale your form intensive site to hold thousands of text boxes and have to ability to remove text boxes all at once. This will make it easier to maintain for future projects as well to any future additions you may have to your site.
Create The Project
If you have not done so, create an empty web site. Open Visual Studio and click File > New > Web Site. Using C# as the installed template, select ASP.NET Empty Web Site, name the file Page and click OK. Right click on the web site name and click Add New Item. Using C# as the template, select Web Form, leave the file name as Default.aspx and click add.
Now that you have a web form to work on, you can start adding items from the toolbox to complete the process. Expand the toolbox panel and grab and place a Button item inside the open and closing div tags. Go ahead and add Three Textboxes as well as a Label Field to add some text. Your code should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <head runat="server"> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Clear Fields" OnClick="Button1_Click" /> </div> </form> </body> </html> |
Pretty straight forward so far. As you can see, the controls will code and set up all the appropriate assets as well as give our button the proper OnClick event.
C# code
To add control and function to your button, simply switch to Split View and double click the Button. Visual Studio will pull up the Default.aspx.cs C# code page. An area is blocked off to input your code for the Button. In order to have the button event clear all text fields, we must construct some logic by using the foreach and if conditional. We will tell the computer to do the following: “Look for each ‘Control’, if the type is ‘TextBox’, then replace the text with ’empty space’, if the text boxes have controls, then clear the text boxes. Your code should look like the example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { CleartextBoxes(this); } public void CleartextBoxes(Control parent) { foreach (Control x in parent.Controls) { if ((x.GetType() == typeof(TextBox))) { ((TextBox)(x)).Text = ""; } if (x.HasControls()) { CleartextBoxes(x); } } } } |
Test the page in your browser and it should now clear all text fields. You can add as many text fields as you like and the button will still clear all fields.
Looking for ASP.NET Hosting on European Server? We can help you a lot!
hostforlifeasp.net is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.