开发者

C#: Value not persisting in postback

开发者 https://www.devze.com 2023-01-31 23:10 出处:网络
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq;
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)
  开发者_StackOverflow社区  {
        txtbxttl.Text = "" + result;

    }
}

In this code the individual value for checkbox is ok but when the total is made it becomes 0.

Please help me to fix it.


The "result" value is not persisted through postbacks. Either recalculate it always when you need the final result or store it in the viewstate.


result variable will not be preerved in numereous post backs because the nature of web application is state less so tore the result in a ViewState or Session variable as shown below.

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

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号