C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Code to clear or reset control values in your web form


Posted Date: 06 Nov 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: PrajeeshMember Level: Gold    
Rating: 1 out of 5Points: 10



In most of the web applications you may need to add a reset button to clear the values of a form which is having dropdown and textboxes, in this situation most of the developers are doing this by adding code like TextBox1.Text =""; and DropDown1.SelectedValue="0", there is a simple solution to do this.

See below code:


public static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)
{
foreach (Control cntrl in pagecontrols)
{
foreach (Control mycontrols in cntrl.Controls)
{
if (txtbox)
{
if (mycontrols is TextBox)
{
(mycontrols as TextBox).Text = string.Empty;
}
}
if (dropdownlist)
{
if (mycontrols is DropDownList)
{
(mycontrols as DropDownList).SelectedIndex = 0;
}
}
if (label)
{
if (mycontrols is Label)
{
(mycontrols as Label).Text = string.Empty;
}
}
}
}
}



You can call the function using following syntax:

FormControl.ResetControls(this.Controls, true, true, false);


Above statement resets all controls except label in the current form



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Resetting Form  .  Controls  .  CodeSnippers  .  Clearing Form  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Validating Images while uploading using reguler expression.
Previous Resource: How to reset all controls in your asp.net Web Form
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use