后台代码
- public partial class AutoAvg : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindData();
- }
- {
- string postback = Request.Form["__EVENTARGUMENT"];
- if (!string.IsNullOrEmpty(postback) && postback.Contains("AutoSave"))
- {
- string[] args = postback.Split('
- );
- AutoSave(args[1]);
- }
- }
- }
- private void BindData()
- {
- Random rnd = new Random();
-
- List<MonthValue> list = new List<MonthValue>();
- for(int i=0;i<10;i++)
- {
- decimal val1 = rnd.Next(100);
- decimal val2 = rnd.Next(100);
- decimal val3 = rnd.Next(100);
- decimal avg =decimal.Round((val1 + val2 + val3 ) / 3,2);
- list.Add(new MonthValue()
- {
- ID = i,
- Month1 = val1,
- Month2 = val2,
- Month3 = val3,
- Avg = avg
- });
- }
- MonthValueList = list;
- gdData.DataSource = list;
- gdData.DataBind();
- }
- private void AutoSave(string index)
- {
- int rowIndex = Convert.ToInt32(index);
- System.Web.UI.WebControls.TextBox txtMonth1 = (System.Web.UI.WebControls.TextBox)gdData.Rows[rowIndex].FindControl("txtMonth1");
- System.Web.UI.WebControls.TextBox txtMonth2 = (System.Web.UI.WebControls.TextBox)gdData.Rows[rowIndex].FindControl("txtMonth2");
- System.Web.UI.WebControls.TextBox txtMonth3 = (System.Web.UI.WebControls.TextBox)gdData.Rows[rowIndex].FindControl("txtMonth2");
- int id = Convert.ToInt32(gdData.DataKeys[rowIndex][0]);
- decimal month1 = 0;
- decimal month2 = 0;
- decimal month3 = 0;
-
- decimal.TryParse(txtMonth1.Text,out month1);
- decimal.TryParse(txtMonth2.Text, out month2);
- decimal.TryParse(txtMonth3.Text, out month3);
- UpdateEntity(id, month1, month2, month3);
- }
- private void UpdateEntity(int id, decimal month1, decimal month2, decimal month3)
- {
- List<MonthValue> list = MonthValueList;
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- if (list[i].ID == id)
- {
- list[i].Month1 = month1;
- list[i].Month2 = month2;
- list[i].Month3 = month3;
- list[i].Avg = decimal.Round((month1 + month2 + month3)/3,2);
- break;
- }
- }
- MonthValueList = list;
- }
- private List<MonthValue> MonthValueList
- {
- get
- {
- if (ViewState["MonthValueList"] == null)
- {
- ViewState["MonthValueList"] = new List<MonthValue>();
- }
- return (List<MonthValue>)ViewState["MonthValueList"];
- }
- set
- {
- ViewState["MonthValueList"] = value;
- }
- }
- }
- [Serializable]
- public class MonthValue
- {
- public int ID { get; set; }
- public decimal Month1 { get; set; }
- public decimal Month2 { get; set; }
- public decimal Month3 { get; set; }
- public decimal Avg { get; set; }
- }
复制代码
|