Tuesday, 12 July 2011

Clear controls contents in a page

 public void clearall(Control pg)
        {
            try
            {
                Control ctrl = new Control();
                ContentPlaceHolder cph = new ContentPlaceHolder();

                foreach (Control ctr in pg.Controls)
                {

                    if (ctr.Controls.Count > 0)
                    {
                        clearall(ctr);
                    }
                    else
                    {
                        string stype = ctr.GetType().ToString();
                        stype = stype.Substring(26);
                        if (stype == "TextBox")
                        {
                            TextBox tb = new TextBox();
                            tb = (TextBox)ctr;
                            tb.Text = "";
                        }
                        if (stype == "DropDownList")
                        {
                            DropDownList ddl = new DropDownList();
                            ddl = (DropDownList)ctr;
                            ddl.ClearSelection();
                        }
                        if (stype == "CheckBoxList")
                        {
                            CheckBoxList cbl = new CheckBoxList();
                            cbl = (CheckBoxList)ctr;
                            cbl.ClearSelection();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

No comments:

Post a Comment