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 »

How to reset all controls in your asp.net 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:

//Function to reset controls
public static void ResetFormControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)
{
//Loop to find page control
foreach (Control cntrl in pagecontrols)
{
foreach (Control mycontrols in cntrl.Controls)
{
//Checking whether the control is a textbox
if (txtbox)
{

if (mycontrols is TextBox) {
{
(mycontrols as TextBox).Text = string.Empty;
}
}
//Checking it is a drop down list or not
if (dropdownlist)
{
if (mycontrols is DropDownList)
{
(mycontrols as DropDownList).SelectedIndex = 0;
}
}
//checking it is a label or not
if (label)
{
if (mycontrols is Label)
{
(mycontrols as Label).Text = string.Empty;
}
}
}
}
}



Responses

Author: venkatesan    12 Nov 2009Member Level: Diamond   Points : 1
Its seems this Resource is Duplicate.

Posting the same Resource as two times.

Pls Avoid this.


Author: Prajeesh    12 Nov 2009Member Level: Gold   Points : 0
Hi venkatesen, It is occurred accidentally when i modified the resource.


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

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: Code to clear or reset control values in your web form
Previous Resource: ViewState
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