Posted by: Suprotim Agarwal
in Category ASP.NET
Step 1: Drag and drop a few controls like textboxes, radio buttons, checkboxes etc. on to the form
Step 2: Add a button to the form and rename its Text property as “Clear all controls using ASP.NET”. Rename its id property to be “btnClearASP”.
Step 3: Double click the button. In its click event, call a method that will clear the content of the controls on a Page.
protected void btnClearASP_Click(object sender, EventArgs e)
Protected Sub btnClearASP_Click(ByVal sender As Object, ByVal e As EventArgs)
Step 4: Write code for this method
private void ResetFormControlValues(Control parent)
foreach (Control c in parent.Controls)
if (c.Controls.Count > 0)
((TextBox)c).Text = "";
((CheckBox)c).Checked = false;
((RadioButton)c).Checked = false;
PrivateSub ResetFormControlValues(ByVal parent As Control)
ForEach c As Control In parent.Controls
If c.Controls.Count > 0 Then
CType(c, TextBox).Text = ""
CType(c, CheckBox).Checked = False
CType(c, RadioButton).Checked = False
The above function is a recursive function that clears the controls values on a page. I am not sure if this will work for a collection control like a RadioButtonList or similar. But I hope you have got some idea of how to write a function to reset contents on a page.
Step 1: Drag and drop a few controls like textboxes, radio button, checkboxes etc. on to the form.
Step 3: Write code for this function 'ClearAllControls()’. In the <head> section of the page, add a <script> tag and write the code for the function as shown below :
<html xmlns="http://www.w3.org/1999/xhtml" >
for (i=0; i<document.forms.length; i++)
doc = document.forms.elements[i];
case "text" :
doc.value = "";
case "checkbox" :
doc.checked = false;
case "radio" :
doc.checked = false;
case "select-one" :
doc.options[doc.selectedIndex].selected = false;
case "select-multiple" :
while (doc.selectedIndex != -1)
indx = doc.selectedIndex;
doc.options[indx].selected = false;
doc.selected = false;
<form id="form1" runat="server">
// Other control come here
// Original Code frumbert
That’s it. Quiet simple, wasn’t it. I would encourage you to play with the code and add to it, by writing some of your own for the other controls on the page.
In this article, we saw how easy it was to reset controls on a page. If you require a specific tip, drop in a line to me at https://www.dotnetcurry.com/Contact.aspx or add your request as a comment on this page. Your comments will drive me to continue this series. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!