Search code examples
c#asp.netuser-controlsdynamicformwizard

Referencing a non-existent user Control?


I have created an application that searches a directory and loads all of the usercontrols into the form and then uses a "GetResult()" method to grab the answers from the form. I did not do this OOP style because I am still learning how to fully utilize OOP and I am now going back to design it with OOP so I can move onto the next part which will be a lot easier if I was working with objects. Right now I have created my "RequestForm" class and I want RequestForm.Result to reach into the UC and call the GetResults() method. I am having a difficult time getting it to do this though due to my lack of knowledge perhaps someone can point me in the right direction.

FormUserControl - Abstract Class

namespace AccessRequest
{
    public abstract class FormUserControl : UserControl
    {
        public abstract string Name();
        public abstract string GetResults();
        public abstract string EmailUsers();
    }
}

RequestForm - Object Class

namespace AccessRequest
{
public class RequestForm
{
    public string Name { get; set; }
    public string ControlID { get; set; }
    public string StepName { get; set; }
    public string FilePath { get; set; }
    public string Results { 
        get
        {
            //How would I pull my usercontrol results with Control.GetReults() from within this area?
            //I have since then turned this into a method. How would I get it to access my UserControl loaded on the asp page to grab the results?
        }
        set;
    }
    public string Emails { get; set; }
    public int Position { get; set; }
    public bool Visible { get; set; }

    public RequestForm()
    {

    }
    /// <summary>
    /// FormResults gathers all needed information about the forms
    /// </summary>
    /// <param name="formName">Name of the Form</param>
    /// <param name="formControlID">ID of the User Control </param>
    /// <param name="wizardStepID">ID of the Wizard Step</param>
    /// <param name="formFilePath">File path of the physical form</param>
    /// <param name="formResults">Results from the form</param>
    /// <param name="formEmails">Other emails to include</param>
    public RequestForm(string formName, string formControlId, string wizardStepID, int wizardStepPosition, string formFilePath, string formResults, string formEmails)
    {
        this.Name = formName;
        this.ControlID = formControlId;
        this.StepName = wizardStepID;
        this.Position = wizardStepPosition;
        this.FilePath = formFilePath;
        this.Results = formResults;
        this.Emails = formEmails;
        this.Visible = false;
    }

    public void SaveList(List<RequestForm> formList)
    {
      //  HttpContext.Current.Session["FormList"] = formList;
    }
}

}

Here is the LoadForms() method I put in OnInit to load all of my forms, I have not fully implemented the RequestForm piece but this is where I believe it should go to builder my object list.

private void LoadForms()
    {
        string dotColor = "Black";
        string formColor = "#808080";

        int loc = 3;
        foreach (ListItem item in chklApplications.Items)
        {
            string formPath = (string)item.Value;

            WizardStepBase newStep = new WizardStep();
            newStep.ID = "wzStep" + item.Text;
            newStep.Title = String.Format("<font color='{0}'>  ¤</font> <font color='{1}'>{2} Request</font>", dotColor, formColor, item.Text);


            var form = LoadControl(formPath);
            form.ID = "uc" + item.Text;
            newStep.Controls.Add(form);
            wzAccessRequest.WizardSteps.AddAt(loc, newStep);

            requestForm.Add(new RequestForm(
                                            item.Text,                      //Form name
                                            form.ID.ToString(),             //User Control ID
                                            newStep.ID.ToString(),          //Wizardstep ID
                                            loc,                            //Wizardstep Position
                                            item.Value.ToString(),          //File Path    
                                            null,                           //Form Results
                                            null                            //Form Emails
                                            ));
            loc++;
        }

    }

Here is where I set whether they are visible or not in my side menu of the Wizard Control. BTW, does anyone know how I can prevent it from even creating the table tags for this? Right now it is growing a large space where I am inserting the forms.

protected void SideBarList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (!ShowWizardStep(e.Item.DataItem))
            {
                e.Item.CssClass = "Hidden";

            }

        }
    }

Thanks for any advise I receive!! :D


Solution

  • Ok, so I figured this out. I already had a method that would gather all the results from the form and then spit them out on the verification screen. I manipulated that code so that now it loads them directly into the object I was working with. Now my objects are full of all the dynamic information I need for my controls and I can manage them a lot easier. Here is the code in case anyone else is looking for the answer.

    MyClass

    public class RequestForm
    {
        public string Name { get; set; }
        public string ControlID { get; set; }
        public string StepID { get; set; }
        public string FilePath { get; set; }
        public string Emails { get; set; }
        public string Results { get; set; }
        public int Position { get; set; }
        public bool Visible { get; set; }
    
        /// <summary>
        /// FormResults gathers all needed information about the forms
        /// </summary>
        /// <param name="formName">Name of the Form</param>
        /// <param name="formControlID">ID of the User Control </param>
        /// <param name="wizardStepID">ID of the Wizard Step</param>
        /// <param name="formFilePath">File path of the physical form</param>
        /// <param name="formResults">Results from the form</param>
        /// <param name="formEmails">Other emails to include</param>
        public RequestForm(string formName, string formControlId, string wizardStepID, int wizardStepPosition, string formFilePath, string formEmails,string formResults, bool formVisible = false)
        {
            this.Name = formName;
            this.ControlID = formControlId;
            this.StepID = wizardStepID;
            this.Position = wizardStepPosition;
            this.FilePath = formFilePath;
            this.Emails = formEmails;
            this.Results = formResults;
            this.Visible = formVisible;
        }
    }
    

    This list that holds all of the controls

        public List<RequestForm> requestForm
        {
            get
            {
                List<RequestForm> requestList = new List<RequestForm>();
                requestList = (List<RequestForm>)Session["RequestForms"];
                var v = Session["RequestForms"];
                return v != null ? (List<RequestForm>)v : null;
            }
            set
            {
                Session["RequestForms"] = value;
            }
        }
    

    This is the method that I use to gather the results and then put them into the object.

        private void GatherFormsData()
        {
            if (requestForm != null)
            {
                foreach (RequestForm rform in requestForm)
                {
                    if (rform.Visible)
                    {
                        WizardStepBase step = (WizardStep)wzAccessRequest.FindControl(rform.StepID);
                        FormUserControl form = (FormUserControl)step.FindControl(rform.ControlID);
                        rform.Results = String.Format("{0}<br>Email: {1}<br><br>", form.GetResults(), form.EmailContact());
                    }
                }
            }
        }
    

    Hope this helps someone.