protected void Page_Load(object sender, EventArgs e)
    {
        int entityTypeValue = 0;

        Int32.TryParse(Request.QueryString["EntityType"], out entityTypeValue);
        if (Enum.IsDefined(typeof(EntityType), entityTypeValue))
        {
            _selectedEntityType = (EntityType)entityTypeValue;
            _entityTypeDefined  = true;
        }

        if (_entityTypeDefined)
        {
            // load edit control for given entity.
            Control      controlToAdd = Page.LoadControl("~/Controls/Edit" + GeneralUtils.EntityTypeToEntityName(_selectedEntityType) + ".ascx");
            IEditControl editor       = controlToAdd as IEditControl;
            if (editor != null)
            {
                editor.EditMode = FormViewMode.ReadOnly;
                phControls.Controls.Add(controlToAdd);
                controlToAdd = Page.LoadControl("~/Controls/Search" + GeneralUtils.EntityTypeToEntityName(_selectedEntityType) + ".ascx");
                ISearchControl searcher = controlToAdd as ISearchControl;
                if (searcher != null)
                {
                    if (Request.QueryString.Count > 1)
                    {
                        // get the filter based on the query string's PK fields. Use the search control for this, as it contains the logic for this.
                        PredicateExpression filter = searcher.CreateFilter(Request.QueryString);
                        editor.FilterToUse = filter;
                        controlToAdd       = Page.LoadControl("~/Controls/ViewRelatedTo" + GeneralUtils.EntityTypeToEntityName(_selectedEntityType) + ".ascx");
                        IViewRelatedControl relatedViewer = controlToAdd as IViewRelatedControl;
                        if (relatedViewer != null)
                        {
                            relatedViewer.FilterToUse = filter;
                            phRelatedEntities.Controls.Add(controlToAdd);
                        }
                    }
                }
            }
        }

        if (!Page.IsPostBack)
        {
            if (_entityTypeDefined)
            {
                this.Title        += _selectedEntityType.ToString() + " instance";
                lblEntityName.Text = GeneralUtils.EntityTypeToEntityName(_selectedEntityType);
            }

            // check for user info
            if (SessionHelper.GetUserID() == string.Empty)
            {
                Response.Redirect("Login.aspx");
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        int entityTypeValue = 0;

        Int32.TryParse(Request.QueryString["EntityType"], out entityTypeValue);
        if (Enum.IsDefined(typeof(EntityType), entityTypeValue))
        {
            _selectedEntityType = (EntityType)entityTypeValue;
            _entityTypeDefined  = true;
        }

        if (_entityTypeDefined)
        {
            // load edit control for given entity.
            Control controlToAdd = Page.LoadControl("~/Controls/Edit" + GeneralUtils.EntityTypeToEntityName(_selectedEntityType) + ".ascx");
            _editor = controlToAdd as IEditControl;
            if (_editor != null)
            {
                _editor.EditMode = FormViewMode.Edit;
                phEditControls.Controls.Add(controlToAdd);
            }
            phEditArea.Visible = false;

            // load search control
            controlToAdd = Page.LoadControl("~/Controls/Search" + GeneralUtils.EntityTypeToEntityName(_selectedEntityType) + ".ascx");
            _searcher    = controlToAdd as ISearchControl;
            if (_searcher != null)
            {
                if (Request.QueryString.Count == 1)
                {
                    // just entitytype specified.
                    _searcher.AllowSingleEntitySearches = true;
                    _searcher.AllowMultiEntitySearches  = false;
                    _searcher.SearchClicked            += new EventHandler(searcher_SearchClicked);
                    phSearchControls.Controls.Add(controlToAdd);
                    phSearchArea.Visible = true;
                }
                else
                {
                    // get the filter based on the query string's PK fields. Use the search control for this, as it contains the logic for this.
                    if (_editor != null)
                    {
                        _editor.FilterToUse  = _searcher.CreateFilter(Request.QueryString);
                        phSearchArea.Visible = false;
                        phEditArea.Visible   = true;
                    }
                }
            }
        }

        if (!Page.IsPostBack)
        {
            if (_entityTypeDefined)
            {
                this.Title        += _selectedEntityType.ToString() + " instance";
                lblEntityName.Text = GeneralUtils.EntityTypeToEntityName(_selectedEntityType);
            }

            // check for user info
            if (SessionHelper.GetUserID() == string.Empty)
            {
                Response.Redirect("Login.aspx");
            }
        }
    }