在下面我已经拿出了不相关的代码。我创建了一个名为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();
}
}
答案 0 :(得分:2)
单击printButton时,它正在使用设置变量时的值DEFAULT
。
private string _printString = "DEFAULT";
你的问题。修改变量时,需要保持printString
的状态。简单地将_printString
分配给另一个值并不会保持更改。您可以编写一个函数,在单击_printString
时将printButton
分配给正确的值,使用ViewState
或Session
或在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"];
答案 1 :(得分:2)
所有(实例)变量都放置在页面生命周期的末尾,因为HTTP是无状态的(甚至是控件)。您可以改用ViewState
,HiddenField
或Session
变量。
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
值未被保留。您需要通过Session或Viewstate将其存储在计算方法中,然后将其设置在打印中,例如: -
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();
}