0

我创建了一个搜索目录并将所有用户控件加载到表单中的应用程序,然后使用“GetResult()”方法从表单中获取答案。我没有采用这种 OOP 风格,因为我仍在学习如何充分利用 OOP,现在我将使用 OOP 进行设计,这样我就可以进入下一个部分,如果我使用对象会容易得多。现在我已经创建了我的“RequestForm”类,我希望 RequestForm.Result 进入 UC 并调用 GetResults() 方法。我很难做到这一点,但由于我缺乏知识,也许有人可以为我指明正确的方向。

FormUserControl - 抽象类

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

RequestForm - 对象类

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;
    }
}

}

这是我在 OnInit 中加载所有表单的 LoadForms() 方法,我还没有完全实现 RequestForm 部分,但我认为它应该去构建我的对象列表。

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++;
        }

    }

这是我在向导控件的侧面菜单中设置它们是否可见的地方。顺便说一句,有谁知道我怎么能阻止它为此创建表格标签?现在它正在增长一个很大的空间,我正在插入表格。

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";

            }

        }
    }

感谢我收到的任何建议!!:D

4

2 回答 2

1

好的,所以我想通了。我已经有一种方法可以收集表单中的所有结果,然后将它们吐出到验证屏幕上。我操纵了该代码,以便现在将它们直接加载到我正在使用的对象中。现在我的对象充满了我的控件所需的所有动态信息,我可以更轻松地管理它们。这是代码,以防其他人正在寻找答案。

我的课

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;
    }
}

此列表包含所有控件

    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;
        }
    }

这是我用来收集结果然后将它们放入对象中的方法。

    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());
                }
            }
        }
    }

希望这可以帮助某人。

于 2010-10-21T12:58:40.027 回答
0

您需要解决几个问题 - 无论是在您的代码中还是在您当前的知识中:-) 从这个开始:

  1. 阅读几篇关于 ASP.NET 页面生命周期的文章,这样您就会更加熟悉在每个生命周期事件处理程序(、、...)中要做OnInit什么OnLoad。好的起点可能是这个MSDN 概述。但是,谷歌搜索“ASP.NET 页面生命周期”并阅读了其他一些文章和示例。

    此外,您还需要熟悉 ASP.NET 页面处理的请求-响应特性。一开始,请记住,当用户点击一些“提交”按钮进而导致发生 HTTP POST 请求时,您有责任创建页面的控制树以匹配其结构、ID 等。上一个请求。否则 ASP.NET 不知道如何在哪些控件中绑定用户填写的数据。

  2. 在标记的行附近,//Lost and confused here :/您正在创建一个新控件并立即查询它的“结果”(我希望它是其中一些编辑框的值)。Page这是行不通的,因为表单从驱动请求处理的对象接收数据很可能为时过早。

  3. 您的Results物业设计不佳。首先,您不应该使用实际“生成”数据的属性,因为它们可以在不通知的情况下更改。属性应用作“智能字段”,否则您最终会得到更难管理和可读性更低的代码。其次,你离开那里的二传手就像“设置”;导致分配给属性的任何值实际上都丢失了,因为没有办法检索它。虽然在极少数情况下这种行为可能是故意的,但在你的情况下,我猜这只是一个错误。

因此,虽然您目前可以放弃任何“好的 OOP 方式”来解决您的问题,但您当然应该更加熟悉页面生命周期。理解它确实需要一些思考 ASP.NET Web 应用程序应该如何工作的原则,但我相信它会为您提供您真正需要的推动力。

更新:关于 Tony 的代码(请参阅评论),应执行以下操作以将代码向前移动:

  1. 属性中的表单数据列表requestForm应该有[表单ASCX路径,控件ID,实际RequestForm数据类]的元组

  2. GatherForms仅在页面最初加载(即)时才应调用,并且应为可用的 ASCX 表单if (Page.IsPostBack)填充requestForm相应的元组。

  3. chklApplications和向导步骤均应根据LoadForms' requestForms 内容创建。

  4. 当要收集结果时,存储在相应requestForm条目中的 ID 可用于查找实际的用户控件。

于 2010-10-07T15:27:03.633 回答