.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Clear value of controls on a page

Posted By:Lakhan Pal Garg       Posted Date: June 23, 2010    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

If you have place your Controls in a container control then you can use the following method to clear the values of all the controls with the help of a single method.
 

In this code you need to pass page's Object and if of container control.

 

public static void ClearControls( Page obj, string strContentHolder)
{
foreach (Control c in obj.Master.FindControl (strContentHolder).Controls)
{

if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{

TextBox t = (TextBox)c;
t.Text ="";
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList"))
{
DropDownList ddl = (DropDownList)c;
ddl.SelectedIndex = 0;
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
{
CheckBox chk = (CheckBox)c;
chk.Checked =false;
}

if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBoxList"))
{
CheckBoxList chkList = (CheckBoxList)c;
for (int i = 0; i < chkList.Items.Count; i++)
{
chkList.Items[i].Selected =false;
}
}
}
}

Thanks & Regards
Lakhan Pal Garg


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend