Pages

Thursday, March 31, 2011

Clear all controls in a ASP.NET Page

This code for Class file (common.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Business
{
public class Common
{
public void ClearControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
ClearControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Text = string.Empty;
}
else if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Checked = false;
}
else if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).SelectedIndex = -1;
}
}
}
}
}
}
————————-
This code for Default.aspx Page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using objects;
using Business;
public partial class Default : System.Web.UI.Page
{
#region Private Variables
Common com = new Common();
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
com.ClearControls(this); //com.ClearControls(this.Form1);
}}

No comments:

Post a Comment