protected void Page_Load(object sender, EventArgs e)
    {
        _session = SamplePortal.Factory.GetWorkSession(this.Session);

        if (_session == null)
        {
            Response.Redirect("Default.aspx");            //If we don't even have a work session open, then the user hasn't chosen anything to be assembled.
        }
        try
        {
            if (!IsPostBack)             // first arrival on this page
            {
                //Advance past the interview.

                //First some housekeeping. The AssembledDocsCache should clean up after itself when it gets disposed.
                // This attempts to remove old documents in case a disposal fails for some reason.
                Util.SweepTempDirectories();

                // Set the max length on the title and description fields.
                // These were previously set in the ASPX page, but ASP.NET drops them for multi-line fields when rendering the page.
                // So we set them manually here using values configurable in the web.config.
                txtTitle.Attributes.Add("maxlength", Settings.MaxTitleLength.ToString());
                txtDescription.Attributes.Add("maxlength", Settings.MaxDescriptionLength.ToString());

                //Get the user-modified interview answers from the HTTP request.
                System.IO.StringReader sr = new StringReader(HotDocs.Sdk.Server.InterviewResponse.GetAnswers(Request.Form));

                //Merge (overlay) the user-modified interview answers back into the assembly's answer collection.
                _session.FinishInterview(HotDocs.Sdk.Server.InterviewAnswerSet.GetDecodedInterviewAnswers(sr));

                if (!string.IsNullOrEmpty(_session.AnswerCollection.FilePath))
                {
                    using (SamplePortal.Data.Answers answers = new SamplePortal.Data.Answers())
                    {
                        DataView ansData = answers.SelectFile(Path.GetFileName(_session.AnswerCollection.FilePath));
                        if (ansData.Count > 0)
                        {
                            // populate answer set title and description
                            txtTitle.Text       = ansData[0]["Title"].ToString();
                            txtDescription.Text = ansData[0]["Description"].ToString();
                        }
                    }
                }

                // Note for HotDocs host application developers: an informational log entry could be added
                // to a log file before (here) and after the call to _session.AssembleDocuments.
                HotDocs.Sdk.Server.Document[] docs  = _session.AssembleDocuments(null);
                AssembledDocsCache            cache = Factory.GetAssembledDocsCache(this.Session);
                foreach (HotDocs.Sdk.Server.Document doc in docs)
                {
                    cache.AddDoc(doc);
                }

                IEnumerable <HotDocs.Sdk.Server.WorkItem> interviewItems = from n in _session.WorkItems where n is HotDocs.Sdk.Server.InterviewWorkItem select n;
                int numberOfInterviews = interviewItems.Count();
                if (numberOfInterviews > 1)
                {
                    IEnumerable <HotDocs.Sdk.Server.WorkItem> completeItems = from n in interviewItems where n.IsCompleted select n;
                    int completedInterviews = completeItems.Count();
                    pnlNextAsm.Visible = true;
                    if (completedInterviews < numberOfInterviews)
                    {
                        lblContinue.Text = String.Format(
                            "You have completed {0} of {1} interviews required for this document set. There {2} {3} follow-up interview{4} still to be done. Click here to proceed to the next interview.",
                            completedInterviews,
                            numberOfInterviews,
                            (numberOfInterviews - completedInterviews) == 1 ? "is" : "are",
                            (numberOfInterviews - completedInterviews).ToString(),
                            (numberOfInterviews - completedInterviews) == 1 ? "" : "s");
                    }
                    else
                    {
                        lblContinue.Text = String.Format(
                            "You have completed {0} of {1} interviews required for this document set. ",
                            completedInterviews,
                            numberOfInterviews);
                        btnNextInterview.Visible = false;
                    }
                }
                else
                {
                    pnlNextAsm.Visible = false;
                }

                //Initialize Doc Grid
                docGrid.DataSource = cache.ToArray();
                docGrid.DataBind();
                panelDocDownload.Visible = (cache.Count > 0);
            }
        }
        catch (Exception ex)
        {
            // Note for HotDocs host application developers: An error entry should be added to a log file here
            // with information about the current exception, including a stack trace:
            System.Diagnostics.Debug.WriteLine("Page_Load: Exception occurred: " + ex.Message);

            if (Settings.DebugApplication)
            {
                throw;
            }
            else
            {
                pnlError.Visible         = true;
                panelDocDownload.Visible = false;

                lblError.Text = "An error prevented the document from being assembled:<br>";
                if (ex.Message.IndexOf("PDF Advantage") > 0)
                {
                    lblError.Text += "HotDocs could not load the required PDF support library.";
                }
                else
                {
                    lblError.Text += ex.Message;
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        _session = SamplePortal.Factory.GetWorkSession(this.Session);

        if (_session == null)
            Response.Redirect("Default.aspx");//If we don't even have a work session open, then the user hasn't chosen anything to be assembled.

        try
        {
            if (!IsPostBack) // first arrival on this page
            {
                //Advance past the interview.

                //First some housekeeping. The AssembledDocsCache should clean up after itself when it gets disposed.
                // This attempts to remove old documents in case a disposal fails for some reason.
                Util.SweepTempDirectories();

                // Set the max length on the title and description fields.
                // These were previously set in the ASPX page, but ASP.NET drops them for multi-line fields when rendering the page.
                // So we set them manually here using values configurable in the web.config.
                txtTitle.Attributes.Add("maxlength", Settings.MaxTitleLength.ToString());
                txtDescription.Attributes.Add("maxlength", Settings.MaxDescriptionLength.ToString());

                //Get the user-modified interview answers from the HTTP request.
                System.IO.StringReader sr = new StringReader(HotDocs.Sdk.Server.InterviewResponse.GetAnswers(Request.Form));

                //Merge (overlay) the user-modified interview answers back into the assembly's answer collection.
                _session.FinishInterview(HotDocs.Sdk.Server.InterviewAnswerSet.GetDecodedInterviewAnswers(sr));

                if (!string.IsNullOrEmpty(_session.AnswerCollection.FilePath))
                {
                    using (SamplePortal.Data.Answers answers = new SamplePortal.Data.Answers())
                    {
                        DataView ansData = answers.SelectFile(Path.GetFileName(_session.AnswerCollection.FilePath));
                        if (ansData.Count > 0)
                        {
                            // populate answer set title and description
                            txtTitle.Text = ansData[0]["Title"].ToString();
                            txtDescription.Text = ansData[0]["Description"].ToString();
                        }
                    }
                }

                // Note for HotDocs host application developers: an informational log entry could be added
                // to a log file before (here) and after the call to _session.AssembleDocuments.
                HotDocs.Sdk.Server.Document[] docs = _session.AssembleDocuments(null);
                AssembledDocsCache cache = Factory.GetAssembledDocsCache(this.Session);
                foreach (HotDocs.Sdk.Server.Document doc in docs)
                    cache.AddDoc(doc);

                IEnumerable<HotDocs.Sdk.Server.WorkItem> interviewItems = from n in _session.WorkItems where n is HotDocs.Sdk.Server.InterviewWorkItem select n;
                int numberOfInterviews = interviewItems.Count();
                if (numberOfInterviews > 1)
                {
                    IEnumerable<HotDocs.Sdk.Server.WorkItem> completeItems = from n in interviewItems where n.IsCompleted select n;
                    int completedInterviews = completeItems.Count();
                    pnlNextAsm.Visible = true;
                    if (completedInterviews < numberOfInterviews)
                    {
                        lblContinue.Text = String.Format(
                            "You have completed {0} of {1} interviews required for this document set. There {2} {3} follow-up interview{4} still to be done. Click here to proceed to the next interview.",
                            completedInterviews,
                            numberOfInterviews,
                            (numberOfInterviews - completedInterviews) == 1 ? "is" : "are",
                            (numberOfInterviews - completedInterviews).ToString(),
                            (numberOfInterviews - completedInterviews) == 1 ? "" : "s");
                    }
                    else
                    {
                        lblContinue.Text = String.Format(
                            "You have completed {0} of {1} interviews required for this document set. ",
                            completedInterviews,
                            numberOfInterviews);
                        btnNextInterview.Visible = false;

                    }
                }
                else
                    pnlNextAsm.Visible = false;

                //Initialize Doc Grid
                docGrid.DataSource = cache.ToArray();
                docGrid.DataBind();
                panelDocDownload.Visible = (cache.Count > 0);
            }
        }
        catch (Exception ex)
        {
            // Note for HotDocs host application developers: An error entry should be added to a log file here
            // with information about the current exception, including a stack trace:
            System.Diagnostics.Debug.WriteLine("Page_Load: Exception occurred: " + ex.Message);

            if (Settings.DebugApplication) throw;
            else
            {
                pnlError.Visible = true;
                panelDocDownload.Visible = false;

                lblError.Text = "An error prevented the document from being assembled:<br>";
                if (ex.Message.IndexOf("PDF Advantage") > 0)
                    lblError.Text += "HotDocs could not load the required PDF support library.";
                else
                    lblError.Text += ex.Message;
            }
        }
    }