ASP.NET - Master Page/Content Page - Clearing controls on content page

Asked By Eric Smith on 16-Aug-13 09:57 AM
I have content page with master page. On the content page I want to clear all text in all textboxes, uncheck any checked checkboxes and set all dropdownlist to index of 0. I created the below method to accomplish this and it works great. I have other pages that will need to use this same functionality. My question is it possible to place this method is a class, create an instance of the class and call the method on other pages. I tried placing in a class in it complains about the word master by underlining it in red - The name "Master" doesn't exit in current context. I have been searching online and haven't found any examples of someone doing this so wondering if it is possible. 

 
private void ClearForm(string ContentPlaceHolder)
  {
    ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl(ContentPlaceHolder);
 
    foreach (Control c in cph.Controls)
    {
    if (c.GetType() == typeof(TextBox))
    {
      ((TextBox)c).Text = "";
    }
 
    if (c.GetType() == typeof(CheckBox))
    {
      ((CheckBox)c).Checked = false;
    }
 
    if (c.GetType() == typeof(DropDownList))
    {
      ((DropDownList)c).SelectedIndex = 0;
    }
    }
  }//end ClearForm
Robbe Morris replied to Eric Smith on 16-Aug-13 12:34 PM
Create a class called BaseMasterPage and have your master pages inherit from it.

public class BaseMasterPage : System.Web.UI.MasterPage

public partial class MasterPage : EqualsSolved.CaedensList.Web.BaseMasterPage


Create a class called BasePage and have all your pages inherit from it.

public class BasePage : System.Web.UI.Page

public partial class ActivityReportDisplay : EqualsSolved.CaedensList.Web.BasePage



Put this in your BasePage class.  It will be you easy access to your BaseMasterPage class methods from any page that uses a master page (doesn't matter which master page).

public BaseMasterPage BaseMasterPage
 {
  get
  {
    return (BaseMasterPage)this.Master;
  }
 }