Home > Asp.net > Reset/Clear all fields – Textbox/Dropdown/Radionbuttonlist/Listbox etc of a web form in Asp.net

Reset/Clear all fields – Textbox/Dropdown/Radionbuttonlist/Listbox etc of a web form in Asp.net


In most of the case after inserting data into database or carrying out any other transactions. in which we have one form that contains bunch of textbox’s, radiobuttonlist, checkboxlist, dropdownlist, checkbox, listbox etc or any other controls in asp.net .now after inserting data, on submit/save click we want to empty/reset/clear all controls in the web form, we actually do like this:

txtid.text =””;
ddid.SelectedIndex = -1;

and so on… and that for each textbox, dropdowns… etc it becomes very tedious and long.. so to make it short we should create a common function to reset all controls in web form…

Here is the ready made function :

Step 1:

///
/// Pankaj lalwani
/// Reset all textbox/radiobutton/dropdowns/listbox
///
///
public static void ClearFields(ControlCollection pageControls)
{
foreach (Control contl in pageControls)
{
string strCntName = (contl.GetType()).Name;
switch (strCntName)
{
case “TextBox”:
TextBox tbSource = (TextBox)contl;
tbSource.Text = “”;
break;
case “RadioButtonList”:
RadioButtonList rblSource = (RadioButtonList)contl;
rblSource.SelectedIndex = -1;
break;
case “DropDownList”:
DropDownList ddlSource = (DropDownList)contl;
ddlSource.SelectedIndex = -1;
break;
case “ListBox”:
ListBox lbsource = (ListBox)contl;
lbsource.SelectedIndex = -1;
break;
}
ClearFields(contl.Controls);
}
}

Step 2:
Call this method on submit/save click after your code ends or sometimes when you provide clear/reset button to empty all fields

ClearFields(Form.Controls);

About these ads
  1. ayesha
    March 31, 2010 at 10:33 am | #1

    awsum…it worked real well…. it tuk me an hour to reach to this code

  2. alex
    September 13, 2010 at 5:10 pm | #2

    exactly what I needed thanks mate!!!

  3. nitin
    April 22, 2011 at 5:08 am | #3

    Thanks a lot dear! you save my time. its working fine.

  4. ahsan
    January 12, 2012 at 10:58 am | #4

    it works good thanks

  5. Chris
    May 9, 2012 at 7:05 pm | #5

    Thanks, saved me a lot of heart ache! Still can’t believe a function like this isn’t already part of ASP.NET.

  6. Manish
    May 18, 2012 at 5:43 am | #6

    Thanks a ton man ! you save my day :)

  7. Mahantesh Surgihalli
    May 30, 2012 at 6:09 am | #7

    Thanks you very much.
    Very nice

  8. Mahantesh Surgihalli
    May 30, 2012 at 6:10 am | #8

    Mahantesh Surgihalli :
    Thank you very much.
    Very nice

  9. monu
    August 9, 2012 at 7:40 am | #9

    thank u so mch dude……its really help full for me coz i have a lot of fields in my web page…..again thanks a lot tooo uuu

  10. rita shoma
    February 3, 2013 at 11:01 am | #10

    thanx a lot Mr Pankaj..
    really thnx a lot

  11. sony
    March 1, 2013 at 6:32 am | #11

    very much useful post.keep updating..

  12. May 8, 2013 at 6:49 am | #12

    It’s very simple to find out any matter on net as compared to books, as I found this article at this website.

  13. May 16, 2013 at 6:14 pm | #13

    Wonderful work! This is the type of information that should
    be shared around the internet. Shame on the search engines
    for not positioning this post higher! Come on over and visit my web site .
    Thanks =)

  14. June 13, 2013 at 8:45 am | #14

    whoah this weblog is great i love reading your posts.
    Keep up the good work! You understand, many persons
    are hunting round for this info, you could help them greatly.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: