1

我有一个 .net 文件,它使用 ac# 来处理一些表单(发布)信息,剖析它,进行计算并创建一个字符串以在屏幕上打印,然后发送电子邮件。

我收到以下错误,但是我只在不同的时间收到它。该页面将工作一个小时(或其他随机时间),然后在一段时间内无法工作。

从我的 .aspx 文件

编译器错误消息:CS0103:当前上下文中不存在名称“sb”

Line 86:        <% Response.Write(sb); %>

这是代码布局。 我包括在内,即使我认为代码没有问题,因为它有时有效,有时无效。就好像某事只是大脑出了问题。我一直在寻找要删除的文件,但找不到任何东西。这是我第一次尝试使用 c#。我们在我的工作场所使用它,并且我已经看过很多代码,但没有尝试自己使用它。

*grade_the_quiz.aspx 的开头*

<%@ Page Language="C#" autoeventwireup="true" CodeFile="automail.aspx.cs" inherits="formsucker" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

结束头/开始站点布局(为简单起见已删除)

<div id="contentleft">
<h1>How do you Compare?</h1>
<p>Compare yourself to your competition - how do you measure up against the best practices benchmark of the most successful Senior Living Caregivers in America?*</P>
    <form id="form1" runat="server">
    <div id="grade">        
        <% Response.Write(sb); %>
</div>         
</form>
</div>

(等,结束 html,#EOF 为简单起见删除)

automail.aspx.cs 的开头

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Text;


public partial class formsucker : System.Web.UI.Page
{


    public void Page_Init(object sender, EventArgs e)
    {
            string reTake = "", answerScore = "", pleaseWork = "", firstname = "", email = "";
            int noAnswer = 0, testVal = 0;

        String sdData = Request["sd"];


        string[] sdValuesSplit = sdData.Split('~');
        foreach (string postValues in sdValuesSplit)
        {
                if (postValues.Contains("FirstName"))
                {
                    firstname = postValues.Split('=')[1];

                }
                if (postValues.Contains("EMail"))
                {
                    email = postValues.Split('=')[1];

                }

                }


        StringBuilder sb = new StringBuilder();

        sb.Append("<p>Below are your results to the quiz " + firstname + ", and we will email them to " + email + " right away!</p>");

           switch (Request["CUSTOM_FORM_FIELD_11111"])
           {
               case "Yes":
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>YES</i></p><p><li> Congratulations!  You are ahead of many!  You understand the value in capturing your prospects/clients critical information.  Capturing information about their history, notes and activity allows information to be shared amongst your team.  You are utilizing a power piece of the puzzle when it comes to keeping up to date with your prospects/clients</li></p>");
                   testVal = testVal + 5;
                   break;
               case "No":
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>NO</i><ul><li> Consider how much time and money it cost to find new leads -- not keeping track of this information can lead to your prospects or clients falling through the cracks.  Marketing to your prospect/client does not work if you don't know what the last activity </li></p>");
                   break;
               default:
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>You Didn\'t Answer the Question</i></li></p>");
                   noAnswer = noAnswer + 1;
                   break;
           }

附加到 sb 的更多 Switch 语句(删除了大约 20 个 switch 语句.. 它正在对测验评分,然后返回不同的语句并将它们附加到 sb。

  StreamWriter sw = new StreamWriter(@"C:\inetpub\websites\occupancyadvantage.com\mailTest.txt", true);
                        try
                        {

                            System.Net.Mail.MailMessage automail = new System.Net.Mail.MailMessage();
                            automail.Subject = "How Do You Compare Quiz Results";
                            automail.To.Add(email);
                            MailAddress address = new MailAddress("XXXXXXXREMOVED", "XXXXXXREMOVED");
                            automail.From = address;
                            automail.IsBodyHtml = true;
                            automail.Body = "<html><body><p>" + sb + "</p><p>XXXXXREMOVED</p>" +
                                "This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited." +
                                "<br /> XXXXXREMOVED Notifications | 2012</body></html>";
                            SmtpClient autosmtp = new SmtpClient("XXXXXREMOVED", 25);
                            NetworkCredential netCred = new NetworkCredential("XXXXXREMOVED", "XXXXXREMOVED");
                            autosmtp.Credentials = netCred;
                            autosmtp.Send(automail);

                            //SmtpMail.SmtpServer = "mail.XXXXXREMOVED.net";
                            //SmtpMail.Send(automail);

                            sw.WriteLine("Success:" + DateTime.Now.ToString());
                            sw.Close();
                            sw.Dispose();

                        }
                        catch (Exception x)
                        {
                            sw.WriteLine("Failure:" + DateTime.Now.ToString());
                            sw.WriteLine(x.ToString() + ":" + x.StackTrace.ToString());
                            sw.Close();
                            sw.Dispose();
                        }




        }
    }

如果你已经走到这一步,我希望我已经提供了最重要的信息。我已经搜索了其中的 20 个左右,并发现了不同的发生方式,只是找不到我的解决方案。我已经在记事本 ++ 中创建了所有内容,所有内容的原始站点都在 VS 中。

4

3 回答 3

2

为方便起见,您始终可以声明一个类级别的字符串,让 Stringbuilder 保持原样,在将所有结果附加到对象之后,将 Stringbuilder 的值分配给类级别的变量。这样,您可以保持 Stringbuilder 不变,但允许访问在页面加载和 init 对象过期后保留的信息。

 public partial class formsucker : System.Web.UI.Page
{
    public String sbG = "";

    protected void Page_Init(object sender, EventArgs e)
    {
            StringBuilder sb = new StringBuilder(); 
            sb.Append("<p>" + allYourAppends + "</p>");
            sbG = sb.ToString();
    }
}
于 2012-07-11T17:52:43.917 回答
0

“sb”的范围仅限于 Page_Init。将其移至 formsucker 类范围,您的错误将(应该)消失。

最有可能的是,在垃圾收集之前,aspx 正在访问 sb,垃圾收集发生在不同的时间,所以这可能是它间歇性的原因。

于 2012-07-11T17:04:46.603 回答
0

当执行超出 Page_Init 时,您的 StringBuilder 对象将过期。在类中声明它,而不是在事件主体中声明它会起作用。

像这样:

 public partial class formsucker : System.Web.UI.Page
 {
     public StringBuilder sb = new StringBuilder();
     public void Page_Init(object sender, EventArgs e)
     {
        string reTake = "", answerScore = "", pleaseWork = "", firstname = "", email = "";
        int noAnswer = 0, testVal = 0;
        //blah blah...
     }
 }

此外,第 86 行:<% Response.Write(sb); %> 应该是:<% Response.Write(sb.ToString()); %>

于 2012-07-11T17:12:41.337 回答