using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class expt2 : System.Web.UI.Page
{
double result ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
result = 0.0;
protected void Chkbxbd_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxbd.Checked)
{
txtbxttl.Text = "" + 10000;
result += double.Parse(txtbxttl.Text);
}
else
result = result - 10000;
}
protected void Chkbxsfa_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxsfa.Checked)
{
txtbxttl.Text = "" + 15000;
result += double.Parse(txtbxttl.Text);
}
else
result = result - 15000;
}
protected void btnttl_Click(object sender, EventArgs e)
{
txtbxttl.Text = "" + result;
}
}
在此代码中,复选框的单个值是正常的,但是当总数达到时,它变为0。
请帮我解决。
答案 0 :(得分:3)
“结果”值不会通过回发持续存在。当您需要最终结果或将其存储在视图状态时,可以重新计算它。
答案 1 :(得分:1)
结果变量不会保留在众多的回发中,因为Web应用程序的性质是状态较少,因此会导致ViewState或Session变量的结果如下所示。
public partial class expt2 : System.Web.UI.Page
{
double result ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
Session["result"] = 0.0;
}
protected void Chkbxbd_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxbd.Checked)
{
txtbxttl.Text = "" + 10000;
result = double.Parse(Session["result"].ToString());
result += double.Parse(txtbxttl.Text);
Session["result"] = result;
}
else
{
result = double.Parse(Session["result"].ToString());
result = result - 10000;
Session["result"]= result;
}
}
protected void Chkbxsfa_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxsfa.Checked)
{
txtbxttl.Text = "" + 15000;
result = double.Parse(Session["result"].ToString());
result += double.Parse(txtbxttl.Text);
Session["result"] = result;
}
else
{
result = double.Parse(Session["result"].ToString());
result = result - 15000;
Session["result"] = result;
}
}
protected void btnttl_Click(object sender, EventArgs e)
{
txtbxttl.Text = "" + Session["result"].ToString();
}
}