变量恢复为初始值

时间:2013-04-08 11:36:30

标签: c# asp.net variables printing response

在下面我已经拿出了不相关的代码。我创建了一个名为printString的字段。 calculateButton_Click方法填充了大量内容,然后我想使用response.write将其发送到打印页面。但是printString变量似乎永远不会停止“默认”。单击printButton_Click时,DEFAULT就会显示在我的空白页面上。修剪下面的代码:

    public partial class _Default : System.Web.UI.Page
    {
        private string _printString = "DEFAULT";

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
        }

        protected void calculateButton_Click(object sender, EventArgs e)
        {
            _printString = "";

_printString = "HARRY POTTER™: THE EXHIBITION Invoice<BR>Today's date: " + DateTime.Today.ToString("dd/MM/yyyy") + "<BR>Visit date: " +
            dateSelectedString + "<BR><BR><BR>Adult tickets: " + numAdult + "<BR>Child tickets: " + numChild + "<BR>Family Passes: " + numFamily +
            "<BR>Payment method: " + paymentType + "<BR>Total to pay: $" + totalPrice.ToString("0.00");

    }

    protected void printButton_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Write(_printString);
        Response.Flush();
        Response.End();
    }
}

3 个答案:

答案 0 :(得分:2)

单击printButton时,它正在使用设置变量时的值DEFAULT

private string _printString = "DEFAULT";

你的问题。修改变量时,需要保持printString的状态。简单地将_printString分配给另一个值并不会保持更改。您可以编写一个函数,在单击_printString时将printButton分配给正确的值,使用ViewStateSession或在printButton点击功能中指定_printString直接如下图所示。

protected void printButton_Click(object sender, EventArgs e)
{
    _printString = "Harry Potter";

    Response.Clear();
    Response.Write(_printString);
    Response.Flush();
    Response.End();
}

将导致Harry Potter被写入页面。

使用Session

protected void calculateButton_Click(object sender, EventArgs e)
{
       _printString = "HARRY POTTER™: THE EXHIBITION Invoice<BR>Today's date: " + DateTime.Today.ToString("dd/MM/yyyy") + "<BR>Visit date: " +
        dateSelectedString + "<BR><BR><BR>Adult tickets: " + numAdult + "<BR>Child tickets: " + numChild + "<BR>Family Passes: " + numFamily +
        "<BR>Payment method: " + paymentType + "<BR>Total to pay: $" + totalPrice.ToString("0.00");

       Session["PrintString"] = _printString;
}

protected void printButton_Click(object sender, EventArgs e)
{
    _printString = (string)Session["PrintString"];

    Response.Clear();
    Response.Write(_printString);
    Response.Flush();
    Response.End();
}

<强>视图状态:

ViewState["PrintString"] = "HarryPotter";

然后要检索您可以执行的操作:

 _printString = (string)ViewState["PrintString"];

http://msdn.microsoft.com/en-gb/library/ms972976.aspx

答案 1 :(得分:2)

所有(实例)变量都放置在页面生命周期的末尾,因为HTTP是无状态的(甚至是控件)。您可以改用ViewStateHiddenFieldSession变量。

private string PrintString 
{
    get
    {
        if (ViewState["PrintString "] == null || string.IsNullOrEmpty((String)ViewState["PrintString"]))
        {
            ViewState["PrintString"] = "DEFAULT";
        }
        return ViewState["PrintString"].ToString();
    }
    set { ViewState["PrintString"] = value; }
}

还有其他选择:

Nine Options for Managing Persistent User State in Your ASP.NET Application

答案 2 :(得分:1)

按钮单击事件导致回发,并且_printString值未被保留。您需要通过SessionViewstate将其存储在计算方法中,然后将其设置在打印中,例如: -

 protected void calculateButton_Click(object sender, EventArgs e)
 {
     _printString = "";
     _printString = "HARRY POTTER™: THE EXHIBITION Invoice<BR>Today's date: " + DateTime.Today.ToString("dd/MM/yyyy") + "<BR>Visit date: " +
        dateSelectedString + "<BR><BR><BR>Adult tickets: " + numAdult + "<BR>Child tickets: " + numChild + "<BR>Family Passes: " + numFamily +
        "<BR>Payment method: " + paymentType + "<BR>Total to pay: $" + totalPrice.ToString("0.00");
     Session["bigstring"] = _printString;
 }

 protected void printButton_Click(object sender, EventArgs e)
 {
     Response.Clear();
     _printString = Session["bigstring"].ToString();
     Response.Write(_printString);
     Response.Flush();
     Response.End();
 }