3

在下面,我删除了不相关的代码。我创建了一个名为printString. 该calculateButton_Click方法做了一堆东西,然后我想使用response.write. 然而,该printString变量似乎并没有停止“默认”。当我单击printButton_Click. 修剪后的代码如下:

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

3 回答 3

2

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

private string _printString = "DEFAULT";

是你的问题。您需要维护printString变量被修改时的状态。简单地分配_printString给另一个值并不会持久化更改。您可以编写一个函数以在单击_printString时分配正确的值printButton,使用ViewState或直接Session在单击函数中分配 _printString,printButton如下所示。

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

于 2013-04-08T11:38:04.210 回答
2

由于 HTTP 是无状态的(即使是控件),所有(实例)变量都在页面生命周期结束时处理。您可以改用ViewState,HiddenFieldSession变量。

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

还有其他选择:

在 ASP.NET 应用程序中管理持久用户状态的九个选项

于 2013-04-08T11:42:18.670 回答
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();
 }
于 2013-04-08T11:45:39.240 回答